Geoapplications development
http://rgeo.wikience.org
Higher School of Economics, Moscow, www.cs.hse.ru
Agenda2
Binary search and DB index3
Approach 1 (straightforward): Sequential scan, O(N)Drawback: slow
Index Data
1 34
2 3
3 12
4 55
5 21
Index Data
2 3
3 12
5 21
1 34
4 55
Approach 2 (optimized): Binary search, O(log N)Drawback: need to maintain index
Intersection problem: approach 14
Approach 1 (straightforward):
for each pair (gi, gj) compute Egenhofer matrix, if it is not empty, gi and gj intersect
Drawback: slow, because operation is complex
Intersection problem: approach 25
Approach 2 (with MBR):
1. for each geometry gi find MBR_i 2. for each (MBR_i, MBR_j) test whether they intersect (fast) 3. if they do, compute Egenhofer matrix, check for intersection pattern (slower)
Significantly reduce the need to perform step 3
Minimum Bounding Rectangle (MBR)6
http://purl.oclc.org/coordinates/a2.htm
3D case – Minimum Bounding Box (MBB)
https://en.wikipedia.org/wiki/Minimum_bounding_boxhttps://en.wikipedia.org/wiki/Minimum_bounding_rectangle
http://www.scriptspot.com/files/u9133/tm_bbox.png
Java Topology Suite: Envelope/MBR Checks7
public boolean crosses(Geometry g) {// short-circuit testif (! getEnvelopeInternal().intersects(g.getEnvelopeInternal()))return false;
return relate(g).isCrosses(getDimension(), g.getDimension());}
public boolean intersects(Envelope other) {if (isNull() || other.isNull()) { return false; }
return !(other.minx > maxx ||other.maxx < minx ||other.miny > maxy ||other.maxy < miny);
}
package com.vividsolutions.jts.geom;
PostGIS Envelope8
http://postgis.net/docs/manual-2.2/ST_Envelope.html
geometry ST_Envelope(geometry g1);Implements:• OpenGIS Simple Features Implementation Specification for SQL 1.1.• SQL/MM specification. SQL-MM 3: 5.1.15
geometry ST_MakeEnvelope(double precision xmin, double precision ymin, double precision xmax, double precision ymax, integer srid=unknown);
Example:
SELECT ST_AsText(ST_Envelope('LINESTRING(0 0, 1 3)'::geometry)); POLYGON((0 0,0 3,1 3,1 0,0 0))
Subset problem9
Approach 1 (straightforward):
Compute intersection of points/geometries with the rectangle
Approach 2:
Geohash10
https://en.wikipedia.org/wiki/Geohash
Geohash: decode example, step 111
https://en.wikipedia.org/wiki/Geohash
Only 5 bits are valid (2^31 = 11111)
u = 26 (decimal) = 11010 (binary)
c = 11 = 01011
f = 14 = 01110
t = 25 = 11001
and so on . . . the resulting sequence of bits is
11010 01011 01110 11001 . . .
Geohash: decode example, step 212
01234 56789
11010 01011 01110 11001 . . .
Odd bits for lat, even bits for lon. Q: why?
left to right:
lat = 1100111101 . . .
lon = 1001101010 . . .
https://en.wikipedia.org/wiki/Geohash
Geohash: decode example, step 313
lat = 1100111101…, lon = 1001101010…
1100111101…
•
•
Geohash: step 214
01234 56789
11010 01011 01110 11001 . . .
Odd bits for lat, even bits for lon. Q: why?
ANSWER:
Lon range = 2 x lon range (twice larger)
https://en.wikipedia.org/wiki/Geohash
Geohash properties: neighborhood16
Geohash limitations17
Source
Geohash algorithms: filter by rectangle18
•
•
source
Geohash algorithms: Ngram Tree Traveral19
source
1. Determine a “good” grid cell size with reasonable overlap of grid to query shape
2. Get the list of overlapping grid cells <g1, g2, …, gN>3. For each grid cell gi:
• Seek all points with geohashes having prefix of gi gird cell
• For each point decode its geohash and intersect with query shape
Geohash algorithms: filter – fixed grid depth20
source
1. Index each point at every grid level :D, DR, DRT, DRT2, DRT2Y
2. Recursive loop across top grid cells:• If a cell is within the query shape, simply
add all assigned entities to the result• If a cell intersects the query shape,
recursive(cell.subcells)
This requires careful implementation
Geohash uses21
GeoHash Java implementations:https://github.com/kungfoo/geohash-javahttps://github.com/astrapi69/jgeohashhttps://github.com/davidmoten/geo
R-Tree22
•
•
•
•
•
Query not intersecting MBR of a given node, cannot intersect any of the contained geometries
R-Tree: challenges23
•
•
R-Tree: one more example24
https://en.wikipedia.org/wiki/R-tree
R-Tree operations: insert25
•
•
•
•
•
R*-Tree, R+Tree26
https://en.wikipedia.org/wiki/R%2B_treehttps://en.wikipedia.org/wiki/R*_tree
JTS and R-Tree27
•
•
PostGIS and R-Tree28
http://postgis.net/docs/manual-2.2/using_postgis_dbmanagement.html#gist_indexes
Problem: efficient read of a 2D slice from disk29
Hilbert Curve30
https://en.wikipedia.org/wiki/Hilbert_curve
Readings31
• https://en.wikipedia.org/wiki/Geohash
•
•
•
Practical task32