[polygons] Separating out simplify polygon into a method in RTree index
This commit is contained in:
@@ -46,11 +46,12 @@ class RTreePolygonIndex(object):
|
|||||||
def index_polygon(self, id, polygon):
|
def index_polygon(self, id, polygon):
|
||||||
self.index.insert(id, polygon.bounds)
|
self.index.insert(id, polygon.bounds)
|
||||||
|
|
||||||
def add_polygon(self, poly, properties, simplify_tolerance=0.0001, preserve_topology=True):
|
def simplify_polygon(self, poly, simplify_tolerance=0.0001, preserve_topology=True):
|
||||||
poly = poly.simplify(simplify_tolerance, preserve_topology=preserve_topology)
|
return poly.simplify(simplify_tolerance, preserve_topology=preserve_topology)
|
||||||
|
|
||||||
|
def add_polygon(self, poly, properties):
|
||||||
if self.include_only_properties:
|
if self.include_only_properties:
|
||||||
properties = {k: v for k, v in properties.iteritems() if k in self.include_only_properties}
|
properties = {k: v for k, v in properties.iteritems() if k in self.include_only_properties}
|
||||||
|
|
||||||
self.polygons.append((properties, prep(poly)))
|
self.polygons.append((properties, prep(poly)))
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
|
|||||||
@@ -105,6 +105,7 @@ class LanguagePolygonIndex(RTreePolygonIndex):
|
|||||||
if poly_type == 'Polygon':
|
if poly_type == 'Polygon':
|
||||||
poly = Polygon(rec['geometry']['coordinates'][0])
|
poly = Polygon(rec['geometry']['coordinates'][0])
|
||||||
index.index_polygon(i, poly)
|
index.index_polygon(i, poly)
|
||||||
|
poly = self.simplify_polygon(poly)
|
||||||
index.add_polygon(poly, dict(rec['properties']))
|
index.add_polygon(poly, dict(rec['properties']))
|
||||||
elif poly_type == 'MultiPolygon':
|
elif poly_type == 'MultiPolygon':
|
||||||
polys = []
|
polys = []
|
||||||
@@ -113,7 +114,8 @@ class LanguagePolygonIndex(RTreePolygonIndex):
|
|||||||
polys.append(poly)
|
polys.append(poly)
|
||||||
index.index_polygon(i, poly)
|
index.index_polygon(i, poly)
|
||||||
|
|
||||||
index.add_polygon(MultiPolygon(polys), dict(rec['properties']))
|
multi_poly = self.simplify_polygon(MultiPolygon(polys))
|
||||||
|
index.add_polygon(multi_poly, dict(rec['properties']))
|
||||||
else:
|
else:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
@@ -127,3 +129,4 @@ class LanguagePolygonIndex(RTreePolygonIndex):
|
|||||||
def get_candidate_polygons(self, lat, lon):
|
def get_candidate_polygons(self, lat, lon):
|
||||||
candidates = OrderedDict.fromkeys(self.index.intersection((lon, lat, lon, lat))).keys()
|
candidates = OrderedDict.fromkeys(self.index.intersection((lon, lat, lon, lat))).keys()
|
||||||
return sorted(candidates, key=self.admin_level, reverse=True)
|
return sorted(candidates, key=self.admin_level, reverse=True)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user