こぼれネット

geometryで記述された点が、geometryで記述された円の中に属するかどうかを反転するselect文は、どっかにあるはず

ca_sim=# select user_id, position_before, position_after from request_log_view;
user_id | position_before | position_after
---------+----------------------------------------------------+----------------------------------------------------
307 | 0101000020E61000003ACC9717607961407CB779E3A4D44140 | 0101000020E61000003EAE0D1563796140B56D1805C1D34140
491 | 0101000020E61000009E5DBEF56179614063EFC517EDD34140 | 0101000020E6100000F7AE415F7A7961406684B70721D44140
57 | 0101000020E61000001A8A3BDE6479614020D1048A58D44140 | 0101000020E610000079909E2287796140FD8348861CD34140
320 | 0101000020E6100000266F80996F79614041F50F2219D44140 | 0101000020E6100000740CC85E6F796140F58079C894D34140
208 | 0101000020E6100000D07B630880796140E84B6F7F2ED44140 | 0101000020E6100000199293895B7961405F5E807D74D44140
60 | 0101000020E6100000751E15FF7779614023A298BC01D44140 | 0101000020E6100000EF75525F96796140EFCA2E185CD34140

見易く、st_astextを使うと、

ca_sim=# select user_id, st_astext(position_before), st_astext(position_after) from request_log_view;
user_id | st_astext | st_astext
---------+-----------------------------+-----------------------------
307 | POINT(139.79298 35.661282) | POINT(139.793345 35.654328)
491 | POINT(139.793208 35.655673) | POINT(139.796188 35.657258)
57 | POINT(139.793563 35.658952) | POINT(139.797746 35.649308)
320 | POINT(139.794873 35.657017) | POINT(139.794845 35.652978)
208 | POINT(139.796879 35.657669) | POINT(139.792424 35.659805)
60 | POINT(139.795898 35.656303) | POINT(139.799606 35.651248)

となる。さて、上記の点が、ある円に属するかどうかを、判定したいんだが ・・・ まだ見つかっていない。

# 絶対にあるはずだ ―― と信じて、今日は寝よう。


起きた。活動開始。

https://myonyomu.hatenablog.com/

PostGIS(地理情報システムを実現するための拡張モジュール)

に、

gisdb=# CREATE INDEX ON t1 USING gist (p1);
CREATE INDEX

gisdb=# SELECT id, ST_AsText(p1) FROM t1 WHERE
  p1 &&
  Box2D(ST_GeomFromText('LINESTRING(139.5 36.5, 139.6 36.6)', 4326));
 id
-----
  11
 323
 503
 663
 669
 683
 804
(7 rows)

を見つけた。

矩形(四角形)に関しては、これでなんとかなりそう。

では、試してみよう。

の、user_idを取り出せるか?

ca_sim=# select user_id, ST_AsText(position_before) from request_log_view WHERE position_before &&  Box2D(ST_GeomFromText('LINESTRING(139.791326 36.663475, 139.795424 35.661496)', 4326));
 user_id |          st_astext
---------+-----------------------------
     149 | POINT(139.792855 35.662321)
     112 | POINT(139.79406 35.662354)
     159 | POINT(139.793198 35.662567)
      12 | POINT(139.792911 35.662442)
     355 | POINT(139.792903 35.662375)
     131 | POINT(139.793011 35.662165)
     490 | POINT(139.793132 35.662414)
     350 | POINT(139.792722 35.662398)
     301 | POINT(139.792798 35.662261)
(9 rows)
ca_sim=#
おお、できた、できた!!
じゃあ今度は、出発地点を"赤"の枠で、到着地点が"青"の枠内のものを探してみよう。

では、

まず、赤の部分と青の部分を指定して・・・

ca_sim=# select user_id, ST_AsText(position_before),ST_AsText(position_after) from request_log_view WHERE (position_before && Box2D(ST_GeomFromText('LINESTRING(139.791326 36.663475, 139.795424 35.661496)', 4326))) && (position_after && Box2D(ST_GeomFromText('LINESTRING(139.780310 35.643812,139.783851 35.641214)', 4326)));
ERROR: operator does not exist: boolean && boolean
LINE 1: ...791326 36.663475, 139.795424 35.661496)', 4326))) && (positi...
^
HINT: No operator matches the given name and argument types. You might need to add explicit type casts.

え、なんで、こうなるの? これで普通いけるよね ..... (ググりながら2~3時間経過)

まさか、&& AND が違うなんてことないよね ―― と思って、試しにやってみたら、

ca_sim=# select user_id, ST_AsText(position_before),ST_AsText(position_after) from request_log_view WHERE (position_before && Box2D(ST_GeomFromText('LINESTRING(139.791326 36.663475, 139.795424 35.661496)', 4326))) AND (position_after && Box2D(ST_GeomFromText('LINESTRING(139.780310 35.643812,139.783851 35.641214)', 4326)));
user_id | st_astext | st_astext
---------+-----------------------------+-----------------------------
149 | POINT(139.792855 35.662321) | POINT(139.782598 35.642426)
(1 row)

―― 通った。

まだ、円形ではないけど、とりあえず当初の目的(に近いとろまで)に達成。

ーーーーー

では、この#149さんの動きを見てみよう。

ca_sim=# select * from user_position_view where id = 149;
date_index | tm | id | x | y | position
------------+---------------------+-----+-----------+-----------+----------------------------------------------------
1 | 2021-01-01 07:57:45 | 149 | 139.79286 | 35.66232 | 0101000020E61000000BEF72115F7961401D3A3DEFC6D44140
1 | 2021-01-01 07:58:00 | 149 | 139.79286 | 35.66232 | 0101000020E61000000BEF72115F7961401D3A3DEFC6D44140
1 | 2021-01-01 07:58:15 | 149 | 139.79286 | 35.66232 | 0101000020E61000000BEF72115F7961401D3A3DEFC6D44140
1 | 2021-01-01 07:58:30 | 149 | 139.79286 | 35.66232 | 0101000020E61000000BEF72115F7961401D3A3DEFC6D44140
1 | 2021-01-01 07:58:45 | 149 | 139.79286 | 35.66232 | 0101000020E61000000BEF72115F7961401D3A3DEFC6D44140
1 | 2021-01-01 07:59:00 | 149 | 139.79286 | 35.66232 | 0101000020E61000000BEF72115F7961401D3A3DEFC6D44140
1 | 2021-01-01 07:59:15 | 149 | 139.79286 | 35.66232 | 0101000020E61000000BEF72115F7961401D3A3DEFC6D44140
1 | 2021-01-01 07:59:30 | 149 | 139.79286 | 35.66232 | 0101000020E61000000BEF72115F7961401D3A3DEFC6D44140
1 | 2021-01-01 07:59:45 | 149 | 139.79286 | 35.66232 | 0101000020E61000000BEF72115F7961401D3A3DEFC6D44140
1 | 2021-01-01 08:00:00 | 149 | 139.79286 | 35.66232 | 0101000020E61000000BEF72115F7961401D3A3DEFC6D44140
1 | 2021-01-01 08:00:15 | 149 | 139.79286 | 35.66232 | 0101000020E61000000BEF72115F7961401D3A3DEFC6D44140
1 | 2021-01-01 08:00:30 | 149 | 139.79286 | 35.66232 | 0101000020E61000000BEF72115F7961401D3A3DEFC6D44140
1 | 2021-01-01 08:00:45 | 149 | 139.79286 | 35.66232 | 0101000020E61000000BEF72115F7961401D3A3DEFC6D44140
1 | 2021-01-01 08:01:00 | 149 | 139.79286 | 35.66232 | 0101000020E61000000BEF72115F7961401D3A3DEFC6D44140
1 | 2021-01-01 08:01:15 | 149 | 139.79286 | 35.66232 | 0101000020E61000000BEF72115F7961401D3A3DEFC6D44140
1 | 2021-01-01 08:01:30 | 149 | 139.79286 | 35.66232 | 0101000020E61000000BEF72115F7961401D3A3DEFC6D44140
1 | 2021-01-01 08:01:45 | 149 | 139.79286 | 35.66232 | 0101000020E61000000BEF72115F7961401D3A3DEFC6D44140
1 | 2021-01-01 08:02:00 | 149 | 139.79286 | 35.66232 | 0101000020E61000000BEF72115F7961401D3A3DEFC6D44140
1 | 2021-01-01 08:02:15 | 149 | 139.79286 | 35.66232 | 0101000020E61000000BEF72115F7961401D3A3DEFC6D44140
1 | 2021-01-01 08:02:30 | 149 | 139.79286 | 35.66232 | 0101000020E61000000BEF72115F7961401D3A3DEFC6D44140
1 | 2021-01-01 08:02:45 | 149 | 139.79298 | 35.66228 | 0101000020E61000000A48FB1F607961408FE1B19FC5D44140
1 | 2021-01-01 08:03:00 | 149 | 139.7931 | 35.66223 | 0101000020E61000008D0C721761796140AE2EA704C4D44140
1 | 2021-01-01 08:03:15 | 149 | 139.79318 | 35.662155 | 0101000020E6100000BC7669C36179614032755776C1D44140
1 | 2021-01-01 08:03:30 | 149 | 139.79326 | 35.66208 | 0101000020E6100000EBE0606F6279614086376BF0BED44140
1 | 2021-01-01 08:03:45 | 149 | 139.7932 | 35.661995 | 0101000020E6100000514D49D661796140F9122A38BCD44140
1 | 2021-01-01 08:04:00 | 149 | 139.7931 | 35.66191 | 0101000020E6100000A54E4013617961409B728577B9D44140
1 | 2021-01-01 08:04:15 | 149 | 139.79301 | 35.661827 | 0101000020E6100000EC2E5052607961400E4E44BFB6D44140
1 | 2021-01-01 08:04:30 | 149 | 139.79291 | 35.66179 | 0101000020E6100000E7FEEA715F79614020ED7F80B5D44140
1 | 2021-01-01 08:04:45 | 149 | 139.79279 | 35.661766 | 0101000020E610000005323B8B5E79614098512CB7B4D44140
1 | 2021-01-01 08:05:00 | 149 | 139.79268 | 35.661743 | 0101000020E61000002F8672A25D796140DF313CF6B3D44140
1 | 2021-01-01 08:05:15 | 149 | 139.79257 | 35.661716 | 0101000020E61000004EB9C2BB5C7961405796E82CB3D44140
1 | 2021-01-01 08:05:30 | 149 | 139.79247 | 35.661694 | 0101000020E6100000780DFAD25B7961409F76F86BB2D44140
1 | 2021-01-01 08:05:45 | 149 | 139.79234 | 35.66167 | 0101000020E610000096404AEC5A796140E75608ABB1D44140
1 | 2021-01-01 08:08:15 | 149 | 139.79189 | 35.660706 | 0101000020E610000009C3802557796140809E060C92D44140
1 | 2021-01-01 08:08:30 | 149 | 139.79202 | 35.659588 | 0101000020E6100000CD76853E58796140217365506DD44140
1 | 2021-01-01 08:08:45 | 149 | 139.79245 | 35.65855 | 0101000020E6100000B3B27DC85B796140F163CC5D4BD44140
1 | 2021-01-01 08:09:00 | 149 | 139.79272 | 35.657696 | 0101000020E6100000DC114E0B5E79614011DF89592FD44140
1 | 2021-01-01 08:09:15 | 149 | 139.79266 | 35.656853 | 0101000020E6100000417E36725D796140C6A354C213D44140
1 | 2021-01-01 08:09:30 | 149 | 139.79323 | 35.655987 | 0101000020E61000006902452C62796140C3482F6AF7D34140
1 | 2021-01-01 08:09:45 | 149 | 139.7939 | 35.65513 | 0101000020E610000060E811A36779614001DD9733DBD34140
1 | 2021-01-01 08:10:00 | 149 | 139.79408 | 35.65424 | 0101000020E6100000F48AA71E6979614075E61E12BED34140
1 | 2021-01-01 08:10:15 | 149 | 139.79427 | 35.65335 | 0101000020E6100000882D3D9A6A796140E9EFA5F0A0D34140
1 | 2021-01-01 08:10:30 | 149 | 139.79384 | 35.652473 | 0101000020E61000005A2BDA1C6779614022C7D63384D34140
1 | 2021-01-01 08:10:45 | 149 | 139.79301 | 35.651573 | 0101000020E61000008D261763607961404276DEC666D34140
1 | 2021-01-01 08:11:00 | 149 | 139.79219 | 35.650677 | 0101000020E6100000B4006DAB5979614093A9825149D34140
1 | 2021-01-01 08:11:15 | 149 | 139.79135 | 35.64979 | 0101000020E610000011363CBD52796140191EFB592CD34140
1 | 2021-01-01 08:11:30 | 149 | 139.7904 | 35.649372 | 0101000020E6100000DAAED0074B796140B56FEEAF1ED34140
1 | 2021-01-01 08:12:00 | 149 | 139.78836 | 35.648926 | 0101000020E61000003C50A73C3A796140454772F90FD34140
1 | 2021-01-01 08:12:15 | 149 | 139.78905 | 35.648067 | 0101000020E6100000E5982CEE3F796140F44E05DCF3D24140
1 | 2021-01-01 08:12:30 | 149 | 139.788 | 35.647343 | 0101000020E610000007EA944737796140E3A9471ADCD24140
1 | 2021-01-01 08:12:45 | 149 | 139.78703 | 35.64754 | 0101000020E6100000D63906642F796140096D3997E2D24140
1 | 2021-01-01 08:13:15 | 149 | 139.7873 | 35.646866 | 0101000020E6100000BDA9488531796140A9F6E978CCD24140
1 | 2021-01-01 08:13:30 | 149 | 139.78622 | 35.64615 | 0101000020E61000005645B8C928796140BB270F0BB5D24140
1 | 2021-01-01 08:13:45 | 149 | 139.78516 | 35.645443 | 0101000020E61000004FE960FD1F79614080BBECD79DD24140
1 | 2021-01-01 08:14:00 | 149 | 139.7842 | 35.645493 | 0101000020E6100000B20FB22C1879614031EA5A7B9FD24140
1 | 2021-01-01 08:14:15 | 149 | 139.78342 | 35.64642 | 0101000020E610000050A73CBA11796140DAC534D3BDD24140
1 | 2021-01-01 08:14:30 | 149 | 139.78328 | 35.64658 | 0101000020E6100000C898BB9610796140849B8C2AC3D24140
1 | 2021-01-01 08:14:45 | 149 | 139.78336 | 35.64649 | 0101000020E610000027874F3A11796140A41CCC26C0D24140
1 | 2021-01-01 08:15:00 | 149 | 139.78343 | 35.646397 | 0101000020E61000007A54FCDF11796140F321A81ABDD24140
1 | 2021-01-01 08:15:15 | 149 | 139.78351 | 35.646305 | 0101000020E6100000CD21A9851279614012A3E716BAD24140
1 | 2021-01-01 08:15:30 | 149 | 139.78358 | 35.64621 | 0101000020E610000020EF552B1379614061A8C30AB7D24140
1 | 2021-01-01 08:15:45 | 149 | 139.78366 | 35.64612 | 0101000020E61000007FDDE9CE1379614080290307B4D24140
1 | 2021-01-01 08:16:00 | 149 | 139.78375 | 35.646027 | 0101000020E6100000D2AA967414796140CF2EDFFAB0D24140
1 | 2021-01-01 08:16:15 | 149 | 139.78383 | 35.64593 | 0101000020E61000002578431A157961401E34BBEEADD24140
1 | 2021-01-01 08:16:30 | 149 | 139.7839 | 35.64584 | 0101000020E61000008466D7BD157961403DB5FAEAAAD24140
1 | 2021-01-01 08:16:45 | 149 | 139.78398 | 35.64575 | 0101000020E6100000D7338463167961408CBAD6DEA7D24140
1 | 2021-01-01 08:17:00 | 149 | 139.78406 | 35.645657 | 0101000020E61000002A01310917796140AC3B16DBA4D24140
1 | 2021-01-01 08:17:15 | 149 | 139.78413 | 35.64556 | 0101000020E610000089EFC4AC17796140FB40F2CEA1D24140
1 | 2021-01-01 08:17:30 | 149 | 139.78423 | 35.64547 | 0101000020E6100000DCBC7152187961401AC231CB9ED24140
1 | 2021-01-01 08:17:45 | 149 | 139.7843 | 35.64538 | 0101000020E61000002F8A1EF81879614069C70DBF9BD24140
1 | 2021-01-01 08:18:00 | 149 | 139.78438 | 35.645287 | 0101000020E61000008257CB9D1979614088484DBB98D24140
1 | 2021-01-01 08:18:15 | 149 | 139.78445 | 35.64519 | 0101000020E6100000E1455F411A796140D74D29AF95D24140
1 | 2021-01-01 08:18:30 | 149 | 139.78453 | 35.6451 | 0101000020E610000034130CE71A796140F6CE68AB92D24140
1 | 2021-01-01 08:18:45 | 149 | 139.78462 | 35.64501 | 0101000020E610000033F9669B1B796140573F36C98FD24140
1 | 2021-01-01 08:19:00 | 149 | 139.78471 | 35.644928 | 0101000020E6100000035B25581C79614059A7CAF78CD24140
1 | 2021-01-01 08:19:15 | 149 | 139.78479 | 35.644833 | 0101000020E61000006249B9FB1C796140A8ACA6EB89D24140
1 | 2021-01-01 08:19:30 | 149 | 139.78487 | 35.64474 | 0101000020E6100000C1374D9F1D796140F7B182DF86D24140
1 | 2021-01-01 08:19:45 | 149 | 139.78494 | 35.64465 | 0101000020E61000002026E1421E79614045B75ED383D24140
1 | 2021-01-01 08:20:00 | 149 | 139.78502 | 35.644554 | 0101000020E61000007F1475E61E79614094BC3AC780D24140
1 | 2021-01-01 08:20:15 | 149 | 139.7851 | 35.644463 | 0101000020E6100000EA23F0871F796140E3C116BB7DD24140
1 | 2021-01-01 08:20:30 | 149 | 139.78517 | 35.644367 | 0101000020E61000004912842B2079614032C7F2AE7AD24140
1 | 2021-01-01 08:20:45 | 149 | 139.78525 | 35.644276 | 0101000020E6100000A80018CF2079614081CCCEA277D24140
1 | 2021-01-01 08:21:00 | 149 | 139.78534 | 35.644184 | 0101000020E610000007EFAB7221796140D0D1AA9674D24140
1 | 2021-01-01 08:21:15 | 149 | 139.78542 | 35.64409 | 0101000020E610000066DD3F16227961401FD7868A71D24140
1 | 2021-01-01 08:21:30 | 149 | 139.78549 | 35.643997 | 0101000020E6100000C5CBD3B9227961406EDC627E6ED24140
1 | 2021-01-01 08:21:45 | 149 | 139.78557 | 35.643906 | 0101000020E610000023BA675D23796140BDE13E726BD24140
1 | 2021-01-01 08:22:00 | 149 | 139.78564 | 35.64381 | 0101000020E61000009AEAC9FC23796140DC627E6E68D24140
1 | 2021-01-01 08:22:15 | 149 | 139.78552 | 35.64375 | 0101000020E610000000E484092379614037E2C96E66D24140
1 | 2021-01-01 08:22:30 | 149 | 139.78542 | 35.643684 | 0101000020E61000003659A31E227961405072874D64D24140
1 | 2021-01-01 08:22:45 | 149 | 139.78531 | 35.64362 | 0101000020E61000006CCEC133217961409886E12362D24140
1 | 2021-01-01 08:23:00 | 149 | 139.78519 | 35.643555 | 0101000020E6100000A243E04820796140B1169F0260D24140
1 | 2021-01-01 08:23:15 | 149 | 139.78508 | 35.64349 | 0101000020E6100000D8B8FE5D1F796140CAA65CE15DD24140
1 | 2021-01-01 08:23:30 | 149 | 139.78497 | 35.643425 | 0101000020E61000000E2E1D731E796140E3361AC05BD24140
1 | 2021-01-01 08:23:45 | 149 | 139.78485 | 35.64336 | 0101000020E610000044A33B881D7961402B4B749659D24140
1 | 2021-01-01 08:24:00 | 149 | 139.78474 | 35.643295 | 0101000020E61000007A185A9D1C79614044DB317557D24140
1 | 2021-01-01 08:24:15 | 149 | 139.78464 | 35.64323 | 0101000020E6100000B08D78B21B7961405D6BEF5355D24140
1 | 2021-01-01 08:24:30 | 149 | 139.78453 | 35.643158 | 0101000020E61000009F3C2CD41A796140C498F4F752D24140
1 | 2021-01-01 08:24:45 | 149 | 139.78442 | 35.64308 | 0101000020E61000005E6743FE19796140E9D66B7A50D24140
1 | 2021-01-01 08:25:00 | 149 | 139.78432 | 35.643013 | 0101000020E61000007079AC1919796140616F62484ED24140
1 | 2021-01-01 08:25:15 | 149 | 139.78418 | 35.64297 | 0101000020E610000059DE550F1879614062A3ACDF4CD24140
1 | 2021-01-01 08:25:30 | 149 | 139.78406 | 35.642925 | 0101000020E61000004E64E60217796140925B936E4BD24140
1 | 2021-01-01 08:25:45 | 149 | 139.78394 | 35.642883 | 0101000020E610000036C98FF815796140C2137AFD49D24140
1 | 2021-01-01 08:26:00 | 149 | 139.7838 | 35.64284 | 0101000020E61000001F2E39EE14796140C347C49448D24140
1 | 2021-01-01 08:26:15 | 149 | 139.78368 | 35.642796 | 0101000020E61000000893E2E313796140F3FFAA2347D24140
1 | 2021-01-01 08:26:30 | 149 | 139.78355 | 35.642754 | 0101000020E6100000FC1873D712796140F433F5BA45D24140
1 | 2021-01-01 08:26:45 | 149 | 139.78342 | 35.642708 | 0101000020E6100000E57D1CCD1179614025ECDB4944D24140
1 | 2021-01-01 08:27:00 | 149 | 139.7833 | 35.642666 | 0101000020E6100000CEE2C5C21079614055A4C2D842D24140
1 | 2021-01-01 08:27:15 | 149 | 139.78317 | 35.642624 | 0101000020E6100000B6476FB80F79614056D80C7041D24140
1 | 2021-01-01 08:27:30 | 149 | 139.78304 | 35.64258 | 0101000020E6100000ABCDFFAB0E7961408690F3FE3FD24140
1 | 2021-01-01 08:27:45 | 149 | 139.78291 | 35.642532 | 0101000020E61000009432A9A10D796140B648DA8D3ED24140
1 | 2021-01-01 08:28:00 | 149 | 139.78279 | 35.64249 | 0101000020E61000007D9752970C796140B77C24253DD24140
1 | 2021-01-01 08:28:15 | 149 | 139.78265 | 35.64245 | 0101000020E610000065FCFB8C0B796140E7340BB43BD24140
(112 rows)

ふむ、移動時間は、約30分というところか。

ゆりかもめを使うとこんな感じ

バスだと、こんな感じ

#149の人をトラッキングしてみました。

QGIS3 →  postGISから「新しいpostGIS接続を作成する」

(その前に、docker psで、DBのポート番号を抑えておくこと 0ba7ddf90bf2 pamtrak06/postgis-pgrouting-osm:latest "docker-entrypoint.s…" 4 hours ago Up 4 hours 0.0.0.0:8911->5432/tcp toyosu_postgres_1)

で、接続に成功したら、メニューの「データベース」から「DBマネージャ」を選択して、以下の順番に入力をしていく。

すると、こんな感じのものが出てくる。

waysを合わせてみるとこんな感じ

さらに、ways_vertices_pgrを重ねると、こんな感じ

#149さんの連続ルートはこれで見える。

select moving_route_log from request_log_view where user_id = 149;

■スマホを見て、走っているのバスに”飛び乗っている”(サービス稼動中の乗車の例)
①待ち時間が0の乗客を抽出する。(到着時刻-出発時刻-所要時間≒0)
到着時刻 request_log_view.time_after
出発時刻 request_log_view.time_before
所要時間 request_log_view.distance_on_foot [meter] と、 request_log_view.distance_by_bus [meter]、バスの速さ 30 km/h、人の速さ 3 km/h から計算する。
②ユーザのidから、移動履歴を取得する。

まず軽く実験 (time_after - time_before < '10 mins'を取り出す)

ca_sim=# select time_after, time_before from request_log_view where time_after - time_before < '10 mins';
time_after | time_before
------------+-------------
10:37:15 | 10:28:00
11:49:30 | 11:41:15
20:36:30 | 20:26:45
22:19:15 | 22:09:45
00:00:00 | 23:48:45
00:00:00 | 23:47:30
00:00:00 | 11:25:45
00:00:00 | 23:48:00
00:00:00 | 23:24:15
00:00:00 | 23:59:15
00:00:00 | 23:39:15
00:00:00 | 23:10:30

時刻と時間を表示させてみる 後半の2つは秒数になっている

ca_sim=# select time_after, time_before, distance_on_foot/0.8333, distance_by_bus/8.333 from request_log_view ;
time_after | time_before | ?column? | ?column?
------------+-------------+--------------------+--------------------
03:37:45 | 03:26:15 | 511.5950243478489 | 84.87666166802921
03:43:30 | 03:22:00 | 494.1959763351471 | 252.84164686899976
03:46:45 | 03:32:00 | 593.0148094595659 | 0
03:52:15 | 03:42:00 | 614.8145339876095 | 0
03:58:45 | 00:27:15 | 1931.1687506562762 | 402.76739975849034

さて、HH:MM:SSを"秒"の表記にする方法は、こうするらしい

ca_sim=# select time_after, time_before, time_after - time_before, (extract(epoch from (time_after - time_before)::time))::numeric from request_log_view;
time_after | time_before | ?column? | date_part
------------+-------------+-----------+-----------
00:28:00 | 00:16:15 | 00:11:45 | 705
00:32:00 | 00:12:15 | 00:19:45 | 1185
00:40:15 | 00:18:45 | 00:21:30 | 1290
00:41:45 | 00:11:45 | 00:30:00 | 1800
00:43:30 | 00:20:45 | 00:22:45 | 1365

では、本命 (到着時間 ー 出発時間 ー 歩行時間 ー 乗車時間 < 60(秒)以内)の乗客をピックアップしろ。

ca_sim=# select user_id,time_after, time_before, time_after - time_before, distance_on_foot, distance_by_bus from request_log_view where extract(epoch from (time_after - time_before)::time)::numeric - distance_on_foot/0.8333 - distance_by_bus/8.333 < 60;
user_id | time_after | time_before | ?column? | distance_on_foot | distance_by_bus
---------+------------+-------------+-----------+------------------+-----------------
234 | 00:28:00 | 00:16:15 | 00:11:45 | 563.23376 | 0
275 | 01:23:30 | 01:12:00 | 00:11:30 | 536.52704 | 0
324 | 03:23:15 | 03:07:45 | 00:15:30 | 731.304 | 0
118 | 03:52:15 | 03:42:00 | 00:10:15 | 512.32495 | 0
57 | 04:16:00 | 03:48:00 | 00:28:00 | 1388.9436 | 0
157 | 07:59:45 | 07:47:15 | 00:12:30 | 618.9883 | 0
458 | 07:59:45 | 07:41:45 | 00:18:00 | 898.75244 | 0
397 | 08:37:00 | 08:09:15 | 00:27:45 | 1363.375 | 0
410 | 08:52:00 | 08:27:30 | 00:24:30 | 1221.9526 | 0
395 | 09:52:15 | 09:41:45 | 00:10:30 | 516.0718 | 0
92 | 09:57:00 | 09:36:30 | 00:20:30 | 1023.69165 | 0
310 | 10:02:15 | 09:50:15 | 00:12:00 | 564.49817 | 0
90 | 10:02:45 | 09:50:45 | 00:12:00 | 596.32117 | 0
155 | 10:28:00 | 10:14:15 | 00:13:45 | 671.9812 | 0
178 | 11:49:30 | 11:41:15 | 00:08:15 | 372.38193 | 0
315 | 12:09:15 | 11:56:15 | 00:13:00 | 643.7695 | 0
481 | 12:11:00 | 11:55:45 | 00:15:15 | 685.71277 | 348.4668
399 | 12:11:45 | 11:49:00 | 00:22:45 | 1123.1423 | 0
127 | 15:00:15 | 14:41:30 | 00:18:45 | 913.04913 | 0
404 | 12:13:30 | 11:41:15 | 00:32:15 | 1562.6414 | 0
198 | 12:15:30 | 11:55:30 | 00:20:00 | 959.8891 | 0
32 | 12:48:15 | 12:27:30 | 00:20:45 | 1014.6684 | 0
116 | 13:06:15 | 12:48:00 | 00:18:15 | 911.0616 | 0
130 | 13:25:00 | 13:05:30 | 00:19:30 | 971.8267 | 0
411 | 15:42:00 | 15:26:00 | 00:16:00 | 788.83246 | 0
229 | 14:07:15 | 13:50:30 | 00:16:45 | 835.96875 | 0
61 | 14:15:45 | 14:01:15 | 00:14:30 | 684.7999 | 0
79 | 14:32:15 | 14:21:45 | 00:10:30 | 494.44864 | 0
317 | 22:20:00 | 22:05:00 | 00:15:00 | 730.14026 | 0
133 | 17:00:30 | 16:31:15 | 00:29:15 | 1454.8848 | 0
97 | 17:18:00 | 17:03:30 | 00:14:30 | 682.81305 | 0
193 | 18:29:30 | 18:05:30 | 00:24:00 | 1199.9945 | 0
80 | 18:55:00 | 18:44:15 | 00:10:45 | 497.08746 | 0
271 | 19:48:45 | 19:36:00 | 00:12:45 | 622.33954 | 0
298 | 19:50:15 | 19:36:00 | 00:14:15 | 694.23785 | 0
13 | 19:55:30 | 19:36:00 | 00:19:30 | 961.26294 | 0
496 | 22:19:15 | 22:09:45 | 00:09:30 | 447.28525 | 0
305 | 21:10:30 | 20:44:45 | 00:25:45 | 1262.3821 | 0
283 | 21:14:00 | 21:00:00 | 00:14:00 | 652.0706 | 0
67 | 21:29:00 | 21:05:45 | 00:23:15 | 1158.776 | 0
124 | 22:43:15 | 22:17:00 | 00:26:15 | 1281.6383 | 0
309 | 23:05:00 | 22:46:15 | 00:18:45 | 921.8486 | 0
338 | 00:00:00 | 23:59:15 | -23:59:15 | 0 | 0
(43 rows)

よし。ラストだ

■歩いて5分のコンビニからの帰宅にも使われている(近距離利用の例)
①出発座標と到着座標の距離が近い、かつ、バスに乗っている ユーザIDを取得する。
出発、到着: request_log_view.position_before、request_log_view.position_after
バスに乗っている: request_log_view.distance_by_bus > 0
②ユーザのidから、移動履歴を取得する。

5分以内は1人もいなかったけど、10分以内に1人だけいた

ca_sim=# select user_id,time_after, time_before, time_after - time_before, distance_on_foot, distance_by_bus from request_log_view where (extract(epoch from (time_after - time_before)::time)::numeric < 600) AND distance_
by_bus/8.333 > 0;
user_id | time_after | time_before | ?column? | distance_on_foot | distance_by_bus
---------+------------+-------------+----------+------------------+-----------------
14 | 10:37:15 | 10:28:00 | 00:09:15 | 92.753555 | 982.5703
(1 row)

 

結局、知り合いの方から教えて貰ったの方法で実現できたみたい (35.65457592, 139.79652749 を中心に300メートル)

ca_sim=# select user_id, ST_AsText(position_before) from request_log_view where st_dwithin(position_before, st_geomfromtext('POINT(139.79652749 35.65457592)', 4326), 0.03233394 / 3600 * 300);
user_id | st_astext
---------+-----------------------------
368 | POINT(139.797258 35.65431)
298 | POINT(139.795197 35.656211)
396 | POINT(139.795056 35.656002)
212 | POINT(139.794265 35.655042)

では、これでファイナル。

ca_sim=# select user_id, ST_AsText(position_before), ST_AsText(position_after) from request_log_view where st_dwithin(position_before, st_geomfromtext('POINT(1
39.79652749 35.65457592)', 4326), 0.03233394 / 3600 * 300) AND st_dwithin(position_after, st_geomfromtext('POINT(139.78482207 35.64501561)', 4326), 0.03233394 / 3600 * 300);
user_id | st_astext | st_astext
---------+-----------------------------+-----------------------------
388 | POINT(139.796825 35.653691) | POINT(139.783568 35.645798)
159 | POINT(139.794932 35.65632) | POINT(139.783022 35.645729)
316 | POINT(139.796816 35.656201) | POINT(139.787289 35.644699)
454 | POINT(139.795517 35.655392) | POINT(139.784164 35.646674)
428 | POINT(139.795264 35.655453) | POINT(139.785816 35.64436)
318 | POINT(139.79846 35.653964) | POINT(139.782998 35.643485)
349 | POINT(139.798028 35.654032) | POINT(139.785334 35.643373)
115 | POINT(139.795711 35.6562) | POINT(139.786663 35.645087)
72 | POINT(139.797807 35.656475) | POINT(139.785665 35.643757)
(9 rows)

忘れない内に、メモを色々残しておこう。

ca_sim=# SELECT home FROM user_param where st_dwithin("home", st_geomfromtext('POINT(139.79652749 35.65457592)', 4326), 0.03233394 / 3600 * 300);
home
----------------------------------------------------
0101000020E6100000B058C3456E796140FB1F60ADDAD34140
0101000020E610000040A6B5696C796140E19524CFF5D34140
0101000020E610000021904B1C79796140A2258FA7E5D34140
0101000020E610000081CCCEA277796140390B7BDAE1D34140

 

以上

 

 

モバイルバージョンを終了