Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use polylabel for address point from polygon #12

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions openaddr/conform.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
from .sample import sample_geojson, stream_geojson

from osgeo import ogr, osr, gdal
from shapely.wkt import loads
from shapely.ops import polylabel
ogr.UseExceptions()

def gdal_error_handler(err_class, err_num, err_msg):
Expand Down Expand Up @@ -728,15 +730,16 @@ def ogr_source_to_csv(source_config, source_path, dest_path):
if source_config.layer == "addresses":
# For Addresses - Calculate the centroid on surface of the geometry and write it as X and Y columns
try:
centroid = geom.PointOnSurface()
shapely_geom = loads(geom.ExportToWkt())
centroid_wkt = polylabel(shapely_geom).wkt
except RuntimeError as e:
if 'Invalid number of points in LinearRing found' not in str(e):
raise
xmin, xmax, ymin, ymax = geom.GetEnvelope()

centroid = ogr.CreateGeometryFromWkt("POINT ({} {})".format(xmin/2 + xmax/2, ymin/2 + ymax/2))
centroid_wkt = "POINT ({} {})".format(xmin/2 + xmax/2, ymin/2 + ymax/2)

row[GEOM_FIELDNAME] = centroid.ExportToWkt()
row[GEOM_FIELDNAME] = centroid_wkt
else:
row[GEOM_FIELDNAME] = geom.ExportToWkt()
else:
Expand Down