2022/08,江端さんの技術メモ

以下のコードは、javascript csvファイルを読み込んで配列化するから丸ごとコピペさせて頂いたものです。

不要なエラー処理やら、ゴチャゴチャしたメソッドとかがなく、実に分かりやすい、素晴しいサンプルコードです。

私の為にコピペさせて頂きました。

<!-----
Javascriptから、csvファイルを読み込む方法

test.csv の中身
test1,test2,test3
a,b,c
1,2,3
----->
<!doctype html>
<html lang="ja">
<head>
    <script>
        // CSVファイルを文字列として取得
        let srt = new XMLHttpRequest();

        srt.open("GET", 'test.csv', false);

        try {
            srt.send(null);
        } catch (err) {
            console.log(err)
        }

        // 配列を用意
        let csletr = [];

        // 改行ごとに配列化
        let lines = srt.responseText.split(/\r\n|\n/);

        // 表示
        console.log(lines)

        // 1行ごとに処理
        for (let i = 0; i < lines.length; ++i) {
            let cells = lines[i].split(",");
            if (cells.length != 1) {
                csletr.push(cells);
            }
        }

        // 表示
        console.log(csletr)
        console.log(csletr[0][0]) // "test1"
        console.log(csletr[2][1]) // "2"
    </script>
</head>
<body>

</body>
</html>

だから、今一度、言おう。『インターネット、ばんざい』

 

2022/08,江端さんの技術メモ

出典 https://www.gixo.jp/blog/327/

最近、よく使われる「アウトカム」という言葉の意味をちょくちょく忘れるので、上記出典よりメモとして画像のみをリンクさせて頂きました。

2022/08,2022/08,江端さんの忘備録

コンプライアンスが叫ばれる昨今です。

Now "Compliance" is more and more important in our society.

今日、Twitterをボンヤリ眺めていたら

Today, when I had seeing Twitter's TL vaguely, I read the following line,

=====

『全然勉強なんかやってないよ」というつよつよエンジニアは、土日もバリバリコード書いてるのにそのことを勉強ではなく「遊び」とか「趣味」とか表現するから、駆け出しエンジニアの人は騙されないでください。

At the time a very strong engineer says "I'm not studying at all,", he/she writes code on weekends as well. They describe it as "fun" or "hobby" instead of studying. Please don't be fooled if you are a novice engineer.

=====

と記載されていて、心底『ドキッ!』としました。

I was really thrilled.

-----

ちなみに、私、今年のゴールデンウイークの9連休の全日、毎日12時間以上、コーディングしていました。

By the way, I was coding for more than 12 hours every day during the entire 9-day Golden Week holiday this year.

しかし、これは、『「遊び」とか「趣味」』です。

However, this is a '"fun" or "hobby".

『楽しかった』から、そう定義して良いと思い込んでいます。

I think that there is no problem to feel fun to do it.

でもって、現在、このコードによって私の頭の中に蓄積された『ノウハウ』は、現在、3つの案件に展開中です。

In addition, the "know-how" accumulated in my mind by this code is currently being deployed in three projects.

-----

私は、このことを公に言わないようにしています(時々、内々では言います)。

I am careful not to say this publicly (but privately).

コンプライアンス的に問題となる蓋然性が高いからです。

This might be occured as complience problem with high probability.

ですので、原則として、沈黙を続けています。

So, principle, I keep it silence.

特に若いエンジニアには、語らないようにしています。

Especially to an youth engineers.

『だって、江端さんが・・・』などと言われたら、私のキャリアは、最終フェーズで崩壊です。

My career would collapse in the final phase if someone said to others, 'Because, Ebata-san...' and so on.

2022/07,江端さんの技術メモ

筋トレには興味ないのですが、『視野に入るところに「鉄棒」があれば、なんとなくぶら下がってみたくなる』というのは、新しい発見でした。

私は、年内に「1回の懸垂」を目標としていたのですが、現在、「20回の懸垂」を、1日2セットやっています。

I had set a goal of "one pull-up" by the end of the year, and now I am doing "20 pull-ups" two sets a day.

仕事が嫌になると、なんとなく部屋の鉄棒にぶら下がっているうちに、こんなことになっていました。

This was the case that I end up hanging out on the bars in my room, whenever I tired to work.

―― リモートオフィス恐るべし

"Remote work, that is amazing"

-----

江端:「今、私は、10代を含めて、人生で一番マッスルだと思う」

Ebata: "Right now, I think I have the most muscle in my life, including my teenage years"

嫁さん:「それは、それで、どうかなと思うぞ」

Wife: "I'm not so sure about that"

2022/07,江端さんの技術メモ

// go get github.com/lib/pq を忘れずに
// go run main12.go

/*
	Channelによるブロックを回避する方法として、Goのタイマー time.Timerで、定期的にブロックを破れるかのテストプログラム
*/

package main

import (
	"fmt"
	"time"

	_ "github.com/lib/pq"
)

var Ch1 chan interface{}

func channel_maker() {
	for {
		time.Sleep(2 * time.Second) // 2秒待つ ()
		Ch1 <- "Ebata is great"
	}
}

func main() {
	Ch1 = make(chan interface{}) // チャネルの初期化
	go channel_maker()

	ping := time.NewTimer(5 * time.Second) // イベントが何もなくても5秒後に発火するようにする
	defer ping.Stop()                      // main()を抜ける前に無効にしておく(なくてもいいかも)

	for {
		select {
		case a := <-Ch1:
			fmt.Println(a)
		case <-ping.C:
			fmt.Println("A ping is coming")
			ping = time.NewTimer(5 * time.Second) // イベントが何もなくても5秒後に発火するようにする
		}
	}
}

うむ・・・ちゃんと動く。困った。

2022/03,江端さんの技術メモ

同じ現象が出て、青冷めていたら、自分の記事がヒットしました。

index.html

http://{s}.tile.osm.org/{z}/{x}/{y}.png → https://{s}.tile.osm.org/{z}/{x}/{y}.png
にしたら、直った

L.tileLayer('https://{s}.tile.osm.org/{z}/{x}/{y}.png', {
detectRetina: true,
maxNativeZoom: 18
}).addTo(map);

すぐに直って、本当によかった。

とは言え、そろそろ地図をローカルに取り込んでおく必要もあるかな・・・

キーワード
tile.osm.org  OSM 表示されない tile

 

2022/07,江端さんの技術メモ

// go run main3.go

/*
	(1)固定長配列を試してみる
*/

package main

import "fmt"

type LocInfo struct {
	Lon float64
	Lat float64
}

func main() {

	var Li [60]LocInfo // 要素0で初期化されている

	for i := 0; i < 60; i++ {
		Li[i].Lon = float64(i)
		Li[i].Lat = float64(i)
	}

	fmt.Println(Li)

	Li[32].Lon = 0.001
	Li[32].Lat = 0.001

	fmt.Println(Li)

}

2022/07,江端さんの技術メモ

// go get github.com/lib/pq を忘れずに
// go run main9.go

/*
	(1)GolangでOpenStreetMap上にマップマッピングするプリミティブな江端式定番マッピング方法
	(http://kobore.net/over90.jpg参照)
*/

package main

import (
	"database/sql"
	"fmt"
	"log"
	"math"

	_ "github.com/lib/pq"
)

var source int
var longitude float64
var latitude float64
var dist float64

func rad2deg(a float64) float64 {
	return a / math.Pi * 180.0
}

func deg2rad(a float64) float64 {
	return a / 180.0 * math.Pi
}

func distance_km(a_longitude, a_latitude, b_longitude, b_latitude float64) (float64, float64) {
	earth_r := 6378.137

	loRe := deg2rad(b_longitude - a_longitude) // 東西  経度は135度
	laRe := deg2rad(b_latitude - a_latitude)   // 南北  緯度は34度39分

	EWD := math.Cos(deg2rad(a_latitude)) * earth_r * loRe // 東西距離
	NSD := earth_r * laRe                                 //南北距離

	distance_km := math.Sqrt(math.Pow(NSD, 2) + math.Pow(EWD, 2))
	rad_up := math.Atan2(NSD, EWD)

	return distance_km, rad_up
}

func diff_longitude(diff_p_x, latitude float64) float64 {

	earth_r := 6378.137
	// ↓ これが正解だけど、
	loRe := diff_p_x / earth_r / math.Cos(deg2rad(latitude)) // 東西
	// 面倒なので、これで統一しよう(あまり差が出ないしね)
	//loRe := diff_p_x / earth_r / math.Cos(deg2rad(35.700759)) // 東西
	diff_lo := rad2deg(loRe) // 東西

	return diff_lo // 東西
}

func diff_latitude(diff_p_y float64) float64 {
	earth_r := 6378.137
	laRe := diff_p_y / earth_r // 南北
	diff_la := rad2deg(laRe)   // 南北

	return diff_la // 南北
}

func main() {
	db, err := sql.Open("postgres", "user=postgres password=password host=localhost port=15432 dbname=utsu_rail_db sslmode=disable")
	if err != nil {
		log.Fatal("OpenError: ", err)
	}
	defer db.Close()

	rows, err := db.Query("SELECT seq,x1,y1 from lrt")
	if err != nil {
		log.Fatal(err)
	}
	defer rows.Close()

	x1, y1 := -1.0, -1.0
	_x1, _y1, _x2, _y2 := -1.0, -1.0, -1.0, -1.0
	px, py := -1.0, -1.0
	flag := 0
	f_flag := 0
	seq := -1

	for rows.Next() {

		if f_flag == 0 { // 初回だけ2二回入力
			if err := rows.Scan(&seq, &x1, &y1); err != nil {
				fmt.Println(err)
			}
			_x1, _y1 = x1, y1
			//fmt.Println(x1, y1)
			f_flag = 1
			continue
		}

		if err := rows.Scan(&seq, &x1, &y1); err != nil {
			fmt.Println(err)
		}
		//fmt.Println(seq, ",", x1, ",", y1)

		_x2, _y2 = x1, y1

		_, rad_up := distance_km(_x1, _y1, _x2, _y2)

		px, py = _x1, _y1

		for {
			// 5.56m/s → 時速20

			px += diff_longitude(0.00556*2*math.Cos(rad_up), py)
			py += diff_latitude(0.00556 * 2 * math.Sin(rad_up))

			//double rad0 = atan2((end_y - start_y),(end_x - start_x));
			//double rad1 = atan2((end_y - test_person.p_y),(end_x - test_person.p_x));

			rad0 := math.Atan2((_y2 - _y1), (_x2 - _x1))
			rad1 := math.Atan2((_y2 - py), (_x2 - px))
			// ここは、http://kobore.net/over90.jpg で解説してある

			if math.Abs(rad0-rad1) >= math.Pi*0.5 {
				// 終点越えの場合、終点に座標を矯正する
				px, py = _x2, _y2
				flag = 1 // フラグを上げろ
			}

			fmt.Println(px, ",", py)

			if flag == 1 {
				flag = 0
				_x1, _y1 = _x2, _y2
				break
			}
		}

	}

	if err := db.Ping(); err != nil {
		log.Fatal("PingError: ", err)
	}

}

2022/07,江端さんの技術メモ

// go get github.com/lib/pq を忘れずに

package main

import (
	"fmt"

	_ "github.com/lib/pq"
)

// GetLoc GetLoc
type GetLoc struct {
	ID    int     `json:"id"`
	Lat   float64 `json:"lat"`
	Lng   float64 `json:"lng"`
	TYPE  string  `json:"type"` // "USER","BUS","CONTROL
	POPUP int     `json:"popup"`
	//Address string  `json:"address"`
}

func person(gl2, gl3 *GetLoc) {

	if gl2.Lng > 0.0 {
		fmt.Println("pass1", gl2)
	} else {
		fmt.Println("pass2", gl2)
	}
}

func person_real() {
	var gl2, gl3 GetLoc

	gl2.Lng = 139.00

	person(&gl2, &gl3)

}

func main() {

	var gl2, gl3 GetLoc

	person(&gl2, &gl3)

	person_real()

}

2022/07,江端さんの技術メモ

の後で、「一体、江端は何を考えているんだ」と思われるかもしれませんが、『宇都宮ライトレールの利用を拒否させるような、ダイクストラをどうやって作ろうか』と考えています ―― しかも、できるだけ手を抜いて。https://wp.kobore.net/%e6%b1%9f%e7%ab%af%e3%81%95%e3%82%93%e3%81%ae%e6%8a%80%e8%a1%93%e3%83%a1%e3%83%a2/post-6473/

ところで、今、いくつかDBを作っていますが、混乱しかけているので、メモを残しておきます。

utsu_tram_db  : 道路と鉄道の強制結合
utsu_tram_db2: 鉄道のコストを下げて、宇都宮ライトレールを優先的に選ばれるようにした
utsu_tram_db3: 宇都宮ライトレールを単線にして、取り扱いをラクにした

ただ、今、ここで、バスが宇都宮ライトレールの上を驀進するようになってきましたので、これを何とかしないといけなくなりました。

ここで逆転の発想で、

utsu_tram_db4: 宇都宮ライトレールを誰も使いたくなくなるくらいに、コストを爆上げしてやればいい

と気がつきました。

で、utsu_tram_db3と、utsu_tram_db4を併用してやれば良い、と気がつきました。


この続きを記載したのですが、反映に失敗したようです。

という訳で簡単に説明しますと、utsu_tram_db3のコスト(現在0.2倍)を、逆に100倍にしたものをutsu_tram_db4として作成しました。現在上手く動いています(色々失敗もしましたが、それを書き残す気力は、もうありません。この週末、20時間以上コーディングしていて、フラフラです)


ちなみに、上記の作業で作ったデータベースを、他の人に渡す為に、以下の作業を行いました。

# pg_dump -U postgres -p 15432 utsu_tram_db3 > utsu_tram_db3.sql
# pg_dump -U postgres -p 15432 utsu_tram_db4 > utsu_tram_db4.sql

で作った、2つのデータベースのダンプ(utsu_tram_db3.sqlと、utsu_tram_db4.sql)を圧縮したのが、こちら。

utsu_tram_db.zip

まず、
create database utsu_tram_db3;
\c utsu_tram_db3
create extension postgis;
create extension pgrouting;
(utsu_tram_db4についても同じ)

としておいてから

これをutsu_tram_db.zipを解凍して、

# psql -U postgres -p 15432 utsu_tram_db3 < utsu_tram_db3.sql
# psql -U postgres -p 15432 utsu_tram_db4 < utsu_tram_db4.sql

で、PostgreSQLにインポートできます(source番号なども完全一致する(はず))。