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

連休中ですが、普段より働いている気がします ―― 疲労を感じます。

I'm on holiday, but I feel like I'm working more than usual -- I feel tired.

「『手を抜くため』の努力は惜しまない」という方針で、以下のようなことで、ずっと悩んでいます。

Based on my policy of "making every effort to 'cut corners'" , I am struggling with following issues.

お願い:PostGISで、曲線のジオメトリをそのまま使って、始点と距離を与えて、終点の座標を得たいです

そういえば、今回、自分で作った本が、自分を助けてくれて、本当に助かっています。

Come to think of it, the book I made myself before really helped me out.

『ありがとう!過去の私!!』と、時空を越えて、自分に感謝しております。

'Thank you, past me!' And I thank myself for crossing time and space.

------

私、最近、teratail(https://teratail.com/)という技術系の質問掲示板を使わせて頂いておりまして ―― 質問ばかりしています。

I have recently been using a technical question board called teratail (https://teratail.com/) -- I ask a lot of questions.

あまりにも申し訳ないので、私でも、答えられることには回答したいと思っています。

I am too sorry to do this, but even I would like to answer what I can.

それが『トレードオフ』・・・ではなくて、えーっと、『共助共存』というやつですよね、うん、そう。

That's the 'trade-off'...No, I don't mean that. Let's see, it's called 'mutual aid coexistence', yes, yes.

しかし、私が答えられそうなことは、「回答済み」か「解決済み」になっており、私に口を出せる余地がありません。

However, anything I might be able to answer is either "answered" or "resolved", leaving no room for me to speak.

-----

『いつか、あなたを助けるから、今はちょっと勘弁ね』と呟きながら ――

With muttering, 'Someday, I'll help you, just not right now,'

今日も、パソコンの前で、宇都宮市のGISデータベース構築と利用方法に、頭を抱えています。

Today, I am sitting in front of my computer with giving my headache, and trying the construction and use of the GIS database for Utsunomiya City.

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

OpenStreetMapのデータからosmiumを使って鉄道だけを抽出しPostGISに入力する方法(宇都宮周辺の鉄道データを使った例)

を、参考にさせて頂いて、試しているのですが、うちのosmiumは、

root@c517fca5dec0:/usr/bin# osmium export -f pg -o utsunomiya-railway-latest.pg utsunomiya-railway-latest.osm.pbf
Set output format with --output-format or -f to 'geojson', 'geojsonseq', or 'text'.

といって、pgファイルを作ってくれません。色々調べたのですが、どうにも上手くいかずに、別の方法を探しています。

で、ちょっと興味本位で、

root@c517fca5dec0:/usr/bin# apt install osm2pgsql

をやってみたら、コンテナの中に、サクッとインストールされたので、これを使う方向で検討しています。

------

>docker container exec -it utsunomiya_db_1 bash

で入ったのですが、osm2pgsqlとかosmiumはインストールできなかったのですが、apt update, apt install, apt upgrade, などを叩き込んで、

>apt install osm2pgsql

>apt install osmium-tool  (×osmium)

>apt install postgresql

を強行しました。

こうしたら、上記のエラー(Set output format with --output-format or -f to 'geojson', 'geojsonseq', or 'text'.)が見えなくなりましたので、osm2pgsqlの方向は見合わせることにしました

よし、続行だ。

この後、アップデートすると動かなくなるので、アップデートを中途半端に行って(完璧にしない)で、pgファイルを作って、インストールに成功。

その後、上記参照ページのSさんから教えて頂いた

utsu_rail_db=# select geom, tags->>'railway' as railway, tags->>'name' as name from utunomiya where tags->>'name' like '%宇都宮ライトレール';

を実施しました。

| construction | (仮称)宇都宮ライトレール
0102000020E6100000070000000725CCB43D7D61408EA2186A5E474240E9CD4D40387D61406409C61C5F474240C6490625317D61407055230560474240197DBBDB2B7D6140A5AA645B61474240BB1171CE237D614050BA3EBD63474240970E8C721F7D6140A95B2CFB644742
4055FBCFF5187D6140B3171B0467474240

| construction | (仮称)宇都宮ライトレール
0102000020E610000008000000BE5B0F15197D61400686072868474240B3075A81217D6140BA50549165474240419B1C3E297D6140B53D30366347424063AD461B2C7D6140685E697462474240CC7C073F317D6140638DC4156147424009E29755337D6140ECB886BE604742
405A2A6F47387D6140E7CD3C146047424069520ABA3D7D6140B130444E5F474240

| construction | (仮称)宇都宮ライトレール
(74 rows)

と、出てきました。

ーーーようやく、宇都宮GISデータベース目処が付きました。

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

「OpenStreetMapのデータから鉄道だけを抽出してGeoJSONで出力する方法」を試してみた件

を前提として、osmから道路と鉄道路線のみを残して残りは消去する方法を探してみました。

どうやら、これでできるようです。

まず、宇都宮地区を抽出します。

osmium extract --bbox 139.76675736787732,36.47004171587971,140.1596765021647,36.659949299138 -o utunomiya.osm.pbf kanto-latest.osm.pbf

次に、以下を行います。

root@2e317961f1d0:/tmp# osmium tags-filter utunomiya.osm.pbf nw/railway nw/highway=motorway,service,primary,secondary,tertiary,unclassified,residential -o utunomiya-lrt-latest.osm.pbf

上記のコマンドのnw(r)は、以下のような意味になっています。

nwr は、OpenStreetMapの nodewayrelationsを指定しています。

nw/(キー)=(タグ),(タグ),(タグ),(タグ),(タグ).....

という記述をします。

(キー)は、https://taginfo.openstreetmap.org/keys を参照

(タグ)は、https://taginfo.openstreetmap.org/tags を参照するといいでしょう。

https://wiki.openstreetmap.org/wiki/JA:Tag:highway%3Dresidential の、

上記をクリックすると、motorway,service,primary,secondary,tertiary,unclassified,residential 

の意味が分かります(多分)。

未分類

どなたか、ご教示頂きたく、お願い致します。

前提

Windows10でDockerを使って、PostgreSQL + PostGISの環境を使っています。

実現したいこと

PostGISで、曲線のジオメトリをそのまま使って、始点と距離を与えて、終点の座標を得たいです。
具体的には、イメージ説明に示すように、

(1)図中のジオメトリで与えられる地図上の曲線において、
(2)始点(A点)の座標(緯度経度)と距離を入力して、
(3)終点(B点)の座標(緯度経度)を取得する

を実現したいです。

検討した事項

geometry型がさっぱり分からん

にも記載しておりますが、曲線のジオメトリは、 ST_Astext()を使うことで、座標に展開できるので、これを使って地道に計算(足し算して端数は比率を乗算する)すれば、算出は可能だと思うのですが、非常に迂遠な計算で、計算負荷も大きくなると思います。

そこで、ジオメトリ(the geom)を直接使って、そのようなことを実現できる関数を
https://postgis.net/docs/manual-3.2/postgis-ja.html#reference
から、探してみたのですが、見付けられませんでした(見落している可能性大です)。

補足情報

大量のエージェントを地図上で動かすシミュレーションで、使うことを想定しており、可能な限りPostGISで提供されている関数とジオメトリをそのまま使って実現したいと考えております。

連絡先

(この質問に関するご報告のみ、ご利用頂きたくお願い致します)

何卒、よろしくお願い致します。

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

「OpenStreetMapから鉄道路線データを落とせない」と愚痴っていたら、社内の人から「OSMに鉄道データ入っていますよ」と言われて、愕然とした件

という訳で、今日は、OpenStreetMapX.jl をダウンロードして色々やっていたのですが、よく分からりませんでした。というか、何ができるのか、が今一つ肚に落ちてこないのです。

で、こちらの路線は諦めて、とにかく鉄道データが取れる手段を探しました。

OpenStreetMapのデータから鉄道だけを抽出してGeoJSONで出力する方法

を試すことにしました。
から、osm.pbfファイルを落してきました。

osmium を使う方法がでていたのですが、これが私のWindows10環境では実現できませんでした。(正確に言うと、私の環境には"cl.exe"というコマンドがないので、コンパイルができない。Visual Studio C++(?)を入れるとできるらしいのですが、これ以上、パソコンの環境を汚したくなったのです)。
で、色々試みたのですが(この間5~6時間)、上手くいかず、dockerでないかなにないかなーと探していたら、見付けました。
1つ目のWindowsのコマンドプロンプト(MSYS64では上手く動かない)かったので、
C:\Users\ebata>docker run -t -i stefda/osmium-tool /bin/bash
で、いきなりDockerのコンテナの中に入りました。

/bin , /usr/bin, /usr/local/bin のどこかで当るだろうと探りを入れていたら、

root@e87f198ccc07:/usr/bin# ls o*
objcopy objdump od odbcinst ogdi-config openssl osage osmium
root@e87f198ccc07:/usr/bin# osmium
Usage: osmium COMMAND [ARG...]

/usr/binで当たりました。

で、作業用のディレクトリを探していたら、/tmpがあったので、ここにファイルを持ち込んで作業することにしました

2つ目の、別のWindowsのコマンドプロンプトを上げて、今動かしているコンテナを調べました

C:\Users\ebata>docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e87f198ccc07 stefda/osmium-tool "/bin/bash" 12 minutes ago Up 12 minutes wonderful_lumiere

で、ローカルからDockerコンテナの/tmpに直接コピーしました。

C:\Users\ebata>docker cp kanto-latest.osm.pbf wonderful_lumiere:/tmp

一つ目のコマンドプロンプトに戻って、ファイルが可能されているか確認しました。

root@e87f198ccc07:/# cd tmp
root@e87f198ccc07:/tmp# ls
kanto-latest.osm.pbf

ちゃんと入っていました。パスも通っているようですので、

root@e87f198ccc07:/tmp# which osmium
/usr/bin/osmium

そのまま、/tmpの中で作業しました

root@e87f198ccc07:/tmp# osmium tags-filter kanto-latest.osm.pbf w/railway -o kanto-railway-latest.osm.pbf
[======================================================================] 100%
root@e87f198ccc07:/tmp# ls
kanto-latest.osm.pbf kanto-railway-latest.osm.pbf
root@e87f198ccc07:/tmp# osmium export kanto-railway-latest.osm.pbf -o kanto-railway-latest.json
root@e87f198ccc07:/tmp# ls
kanto-latest.osm.pbf kanto-railway-latest.json kanto-railway-latest.osm.pbf

2つ目のコマンドプロンプトを立ち上げて、dockerコンテナから、ローカルに、完成したjsonファイルを送り出します。

C:\Users\ebata\docker-osmium-tool>docker cp wonderful_lumiere:/tmp/kanto-railway-latest.json .

ローカルの方に変換されたファイルが戻ってきました。
C:\Users\ebata>ls
 Dockerfile   README.md   kanto-latest.osm.pbf   kanto-railway-latest.json   work
さて、これを、ドラッグして、QGISに放り込んで出てきた図です。

見事に鉄道だけが抽出されています。

しかし、重要なのはそこではありません。

芳賀・宇都宮LRT が、すでに表示されている ―― これは、今の私にとって、とてつもないインパクトなのです。

さて、今回はLRTを中心とした宇都宮市をカバーする地域の交通機関を切り出します。

【食べログ作れる? 】 OpenStreetMap から Osmiumで飲食店の位置情報を取得してGeoJSONに出力してみたを参照して、

$ osmium extract --bbox 左上の経度,左上の緯度,右下の経度, 右下の緯度 -o 出力したいファイル名.pbf 元のファイル名.osm.pbf

から、

$ osmium extract --bbox 139.76675736787732, 36.659949299138, 140.1596765021647, 36.47004171587971 -o utunomiya-lrt.osm.pbf kanto-latest.osm.pbf

として切り出してみます。

root@80e1a2d0dc02:/tmp# osmium extract --bbox 139.76675736787732, 36.659949299138, 140.1596765021647, 36.47004171587971 -o utunomiya-lrt.osm.pbf kanto-latest.osm.pbf
Error parsing command line: too many positional options have been specified on the command line

あれ、コンマと空白がダメだったかな?

root@80e1a2d0dc02:/tmp# osmium extract --bbox 139.76675736787732,36.659949299138,140.1596765021647,36.47004171587971 -o
utunomiya-lrt.osm.pbf kanto-latest.osm.pbf
Need LEFT < RIGHT and BOTTOM < TOP in --box/-b option.

うむ、 では、36.659949299138 と 36.47004171587971 を入れかえて、再度挑戦

root@80e1a2d0dc02:/tmp# osmium extract --bbox 139.76675736787732,36.47004171587971,140.1596765021647,36.659949299138 -o utunomiya-lrt.osm.pbf kanto-latest.osm.pbf
[======================================================================] 100%

成功したっぽいです。

root@80e1a2d0dc02:/tmp# osmium tags-filter utunomiya-lrt.osm.pbf w/railway -o utunomiya-railway-latest.osm.pbf
[======================================================================] 100%

root@80e1a2d0dc02:/tmp# osmium export utunomiya-railway-latest.osm.pbf -o utunomiya-railway-latest.json

では、ここから、2つ目のコマンドプロンプトから、

C:\Users\ebata\docker-osmium-tool>docker cp serene_shirley:/tmp/utunomiya-railway-latest.json .
C:\Users\ebata\docker-osmium-tool>docker cp serene_shirley:/tmp/utunomiya-lrt.osm.pbf .

としました。

utunomiya-railway-latest.json の表示結果はこちらです。

データ量も、関東全域と比較すると、11%程度になりました。


ところで、osmファイルと、osm.pbfファイルの違いって何だろう?

 

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

「OpenStreetMapから鉄道路線データを落とせない」と愚痴っていたら、社内の人から「OSMに鉄道データ入っていますよ」と言われて、愕然としました。


江端:「私は、鉄道のOSMデータの取り方に辿り付けていません。いつも、ここからOSMデータをダウンロードしていますが、地理情報しか取れていない(ように思えます)。お暇な時に御教授頂けましたら幸いです。」

同僚:「そこから取れるデータの中に、鉄道路線のデータも入っています」

江端: ―― はい?

江端:「かなりずうずうしいお願いをしていることは分かっているのです、このタグをPostGISのpostgresqlに落す方法とかご存知だったりしますか? 多分、この辺(Configuration file = /usr/local/share/osm2pgrouting/mapconfig_for_cars.xml)です。」

同僚:「私は、JuliaのOpenStreetMapX.jlを少し改造して、直接OpenStreetMapのosmファイルをパースしていました」

https://github.com/pszufe/OpenStreetMapX.jl


で、現在、readme.mdを読んでいます。

  • Open Street Mapデータの空間解析・シミュレーション・可視化用パッケージ(プロット機能は別パッケージで提供されます)
  • このパッケージの目的は、都市のマルチエージェントモデリングとシミュレーションのためのバックボーンを提供することです。
  • このパッケージは *.osm と *.pbf (@blegat によって寄贈) ファイルを解析し、メタデータに沿った Graphs.jl 表現を生成することができます。

うむ、この連休の使い方としては、有意義な時間を過せそうです。

ところで、Juliaって、コンピュータ言語かな。これ以上、言語は、覚えたくないんだけど ―― 仕方ないですね。

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

昔、倒れている人を助けようとして『余計なことするな!』と怒鳴られたことがあります。

I once tried to help a man who had fallen and was yelled at, 'Don't do anything unnecessary!

結構ショックだったので、覚えています。

It was quite a shock, so I remember it.

ただ、私も人の手を素直に借りれない性分であるので、その心情、ちょっとだけ理解できます。

However, I too am of the nature that I can't honestly ask for help from others, so I can understand the mind a little bit.

-----

数年前、駅で倒れている人がいました。

A few years ago, I saw a man lying in a train station.

皆、心配そうにはするのですが、具体的に手を差し延べることはしないようでした。

Everyone seemed concerned, but did not seem to reach out specifically for help.

私も、原則、そちらの側ですが、それでも倒れた人の近くにしゃがんで、一言言いました。

As a rule, I am on their side, but I still crouched down near the fallen man and said a few words.

『もし、ご希望があれば、お助けします。どうしますか?』

"If you wish, we can help you. Which is better for you?"

私は、ここで遠慮した人を「見捨てる」と決めています。

I have decided to "ditch" anyone who refrains here.

セカンドチャンスは提供しません。

I do not offer second chances.

これが、『江端ファースト&ファイナルコールプロトコル』です。

This is the "Ebata First & Final Call Protocol".

ちなみに、その時は、その人が『駅員を呼んでくれ』と頼んだので、駅員を呼んで、そのまま立ち去りました。

Incidentally, at that time, the man asked me to 'call the station staff,' so I called the station staff and just walked away.

-----

私は、

I always ask, at least once, the following questions.

「席をおゆずりしましょうか」

"Shall I give you a seat?"

「救急車を呼びましょうか」

"Shall I call an ambulance?"

「飲料水、買ってきましょうか」

"Shall I get you some drinking water?"

"If you need help, I will give it to you."

"If you need help, I will give it to you."

と、最低一回は必ず尋ねます。

しかし、自分の言葉でご辞退されれば、私は立ち去ります。

However, if they 'decline' on their own terms, I will walk away.

(応答がない場合は、状況の判断に因ります)

(If no response is received, the decision will be based on the situation)

-----

ただし、「駅で飛び込み自殺をしようとしている人」に関しては、このプロトコルの例外です。

However, an exception to this protocol is made with regard to "a person attempting to commit suicide by jumping into a train station".

従わない場合は、力づくでも止めます。

If she/he does not comply, I will stop them, even by force.

『飛び込むなら、私の載った電車の次の電車にしてくれ』

"If you're going to jump in, make sure it's the next train after the one I'm on."

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

この連休でやっておきたいことをメモに書き出してみたら、とんでもない数になりました。

I wrote down in my notes what I wanted to do during this holiday weekend, then, there were a tremendous number of things to do.

―― どうして、私(たち)は、連休に、こんなに過大な要求を自分に課せるのか?

"Why do I (we) put such excessive demands on myself(ourselves) during the holidays?"

言わば『連休幻想』とでも言うものでしょうか。

Is it a "holiday illusion," so to speak?

私たちに与えられた時間と、私たちの持っている能力が、連休の期間に跳ね上がる訳でもありません。

The time we are given and the abilities we have, do not jump during the holidays.

メリットは、『メールと電話がやってこない』くらいなんですけどね。

The only advantage is that 'emails and phone calls don't come to us'.

-----

私は、GW初日から、ドップリとコーディングをしています。

I have been coding deeply since the first day of GW.

私の人工知能(技術)を搭載した3台のバスたちは、移動・連携しながら、相互に一秒間に数百回のルート計算を繰返し、街(先月視察してきた)の、住人のピックアップとドロップオフを続けています。

Three buses equipped with my artificial intelligence (technology) are moving and working together, mutually calculating their routes hundreds of times per second and continuing to pick up and drop off residents of the city (which I inspected last month).

もちろん、Webの地図上で、ですが。

On a web map, of course.

―― 可愛いやつらめ

"Cute little guys"

と、目を細めています。

I am looking at them with smile.

嫁さんも、動いているアイコンを見て「可愛いね」と言っていますが ―― 多分、その見方は、全然違うんだろうなぁ、と思っています。

My wife also looks at the moving icons and says they are cute, however I think that her viewpoint is different from mine.