1

I have some LINESTRING in PostGIS.

 LINESTRING(104.011 30.637,104.014 30.638,104.022 30.640,104.026 30.645)
 LINESTRING(104.022 30.666,104.031 30.659)
 LINESTRING(104.044 30.655,104.056 30.655)

I want to get all the geohash that intersects LINESTRING on level 6 (or others). So I excepted result like:

[wm3yqc,wm3yr1,wm3yr4]
[wm3yrn,wm3yrj,wm3yrm]
[wm3yrs,wm3yru]

I know ST_GeoHash to get geohash in PostGIS. But as the document says:

For non-points, the starting point of the calculation is the center of the bounding box of the geometry.

Add

There is a bug to compute a geohash for vertices in adding vertices by ST_Segmentize method. As shown in the figure below, if parameter max_segment_length of ST_Segmentize is larger than the length of LINESTRING passing through the grid geohash, it will be omitted. enter image description here

The length of the length of LINESTRING passing through the grid geohash may be close to zero. We can't avoid this result by adjusting the parameters.

Does anyone have a good solution? It is also acceptable to use geopandas or shapely without complex loop.

5
  • 1
    georaptor? github.com/ashwin711/georaptor (geohash polygons)
    – Mapperz
    Commented Feb 27, 2019 at 16:16
  • @Mapperz The input required for georaptor is a set of geohash, which is exactly what I want to get result. It can't meet my needs. Commented Feb 28, 2019 at 6:08
  • 1
    1) Determine your area of interest 2) Create polygons of all geohashes needed to cover that AOI with blog.tafkas.net/2018/09/28/creating-a-grid-based-on-geohashes 3) Store the geohash polygon layer into PostGIS 4) Select polygons intersected by lines with ST_Intersects.
    – user30184
    Commented Feb 28, 2019 at 7:17
  • @user30184 Maybe this is the final solution. Thank you again. Commented Feb 28, 2019 at 10:27
  • It might be possible to do everything in PostGIS. 1) Create a point grid with suitable spacing 2) Compute geohashes for them 3) Create polygons for geohashes with postgis.net/docs/ST_GeomFromGeoHash.html
    – user30184
    Commented Feb 28, 2019 at 10:58

1 Answer 1

5

You can extract the vertices from your linestrings and let PostGIS to compute a geohash for each.

select ST_GeoHash((g.gdump).geom) as geohash
from
(select ST_DumpPoints(ST_GeomFromText('LINESTRING(104.011 30.637,104.014 30.638,104.022 30.640,104.026 30.645)')) as gdump) as g;

Result:
"wm3yqc63rywzdjsm3vp9"
"wm3yqckxmc5ubvje34ve"
"wm3yr1fdbmcp27grpn80"
"wm3yr4v2j6p9s14kvgtk"

You can control the number of vertices in geometries at least with ST_Segmentize (add points) and ST_SnapToGrid (combine close points).

1
  • Thanks for your answer. This essence is to calculate LINESTRING's geohash by vertices , but because the limitation of max_segment_length 's , doing so will lead to missing. You can see what I added to the question. Commented Feb 28, 2019 at 6:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.