def to_geojson(regions): # pragma: no cover features = [] for code, region in regions.items(): # calculate the maximum radius of each polygon boundary = region.boundary if isinstance(boundary, shapely.geometry.base.BaseMultipartGeometry): boundaries = list(boundary.geoms) else: boundaries = [boundary] radii = [] for boundary in boundaries: # flip x/y aka lon/lat to lat/lon boundary = numpy.fliplr(numpy.array(boundary)) ctr_lat, ctr_lon = geocalc.centroid(boundary) radii.append(geocalc.max_distance(ctr_lat, ctr_lon, boundary)) radius = round(max(radii) / 1000.0) * 1000.0 features.append({ 'type': 'Feature', 'properties': { 'alpha2': code, 'radius': radius, }, 'geometry': shapely.geometry.mapping(region), }) def _sort(value): return value['properties']['alpha2'] features = sorted(features, key=_sort) lines = ',\n'.join([json.dumps(f, sort_keys=True, separators=(',', ':')) for f in features]) return '{"type": "FeatureCollection", "features": [\n' + lines + '\n]}\n'
def to_geojson(regions): # pragma: no cover features = [] for code, region in regions.items(): # calculate the maximum radius of each polygon boundary = region.boundary if isinstance(boundary, shapely.geometry.base.BaseMultipartGeometry): boundaries = list(boundary.geoms) else: boundaries = [boundary] radii = [] for boundary in boundaries: # flip x/y aka lon/lat to lat/lon boundary = numpy.fliplr(numpy.array(boundary)) ctr_lat, ctr_lon = geocalc.centroid(boundary) radii.append(geocalc.max_distance(ctr_lat, ctr_lon, boundary)) radius = round(max(radii) / 1000.0) * 1000.0 features.append({ 'type': 'Feature', 'properties': { 'alpha2': code, 'radius': radius, }, 'geometry': shapely.geometry.mapping(region), }) def _sort(value): return value['properties']['alpha2'] features = sorted(features, key=_sort) lines = ',\n'.join([ json.dumps(f, sort_keys=True, separators=(',', ':')) for f in features ]) return '{"type": "FeatureCollection", "features": [\n' + lines + '\n]}\n'
def to_geojson(regions): # pragma: no cover features = [] features_buffered = [] for code, region in regions.items(): # calculate the maximum radius of each polygon boundary = region.boundary if isinstance(boundary, shapely.geometry.base.BaseMultipartGeometry): boundaries = list(boundary.geoms) else: boundaries = [boundary] radii = [] for boundary in boundaries: # flip x/y aka lon/lat to lat/lon boundary = numpy.fliplr(numpy.array(boundary)) # calculate centroid ctr_lat, ctr_lon = boundary.mean(axis=0) ctr_lat = float(ctr_lat) ctr_lon = float(ctr_lon) radii.append(geocalc.max_distance(ctr_lat, ctr_lon, boundary)) radius = round(max(radii) / 1000.0) * 1000.0 features.append({ 'type': 'Feature', 'properties': { 'alpha2': code, 'radius': radius, }, 'geometry': shapely.geometry.mapping(region), }) # Build up region buffers, to create shapes that include all of # the coastal areas and boundaries of the regions and anywhere # a cell signal could still be recorded. The value is in decimal # degrees (1.0 == ~100km) but calculations don't take projection # / WSG84 into account. # After buffering remove any parts that crosses the -180.0/+180.0 # longitude boundary to the east or west. buffered = (region.buffer(0.5).simplify(0.02).difference( DATELINE_EAST).difference(DATELINE_WEST)) features_buffered.append({ 'type': 'Feature', 'properties': { 'alpha2': code, }, 'geometry': shapely.geometry.mapping(buffered), }) def _sort(value): return value['properties']['alpha2'] def _to_collection(features): # put each region on a single line to get a diffable output lines = ',\n'.join([ json.dumps(feature, sort_keys=True, separators=(',', ':')) for feature in sorted(features, key=_sort) ]) return ('{"type": "FeatureCollection", ' '"features": [\n' + lines + '\n]}\n') return (_to_collection(features), _to_collection(features_buffered))
def to_geojson(regions): # pragma: no cover features = [] features_buffered = [] for code, region in regions.items(): # calculate the maximum radius of each polygon boundary = region.boundary if isinstance(boundary, shapely.geometry.base.BaseMultipartGeometry): boundaries = list(boundary.geoms) else: boundaries = [boundary] radii = [] for boundary in boundaries: # flip x/y aka lon/lat to lat/lon boundary = numpy.fliplr(numpy.array(boundary)) ctr_lat, ctr_lon = geocalc.centroid(boundary) radii.append(geocalc.max_distance(ctr_lat, ctr_lon, boundary)) radius = round(max(radii) / 1000.0) * 1000.0 features.append({ 'type': 'Feature', 'properties': { 'alpha2': code, 'radius': radius, }, 'geometry': shapely.geometry.mapping(region), }) # Build up region buffers, to create shapes that include all of # the coastal areas and boundaries of the regions and anywhere # a cell signal could still be recorded. The value is in decimal # degrees (1.0 == ~100km) but calculations don't take projection # / WSG84 into account. # After buffering remove any parts that crosses the -180.0/+180.0 # longitude boundary to the east or west. buffered = (region.buffer(0.5) .simplify(0.02) .difference(DATELINE_EAST) .difference(DATELINE_WEST)) features_buffered.append({ 'type': 'Feature', 'properties': { 'alpha2': code, }, 'geometry': shapely.geometry.mapping(buffered), }) def _sort(value): return value['properties']['alpha2'] def _to_collection(features): # put each region on a single line to get a diffable output lines = ',\n'.join([json.dumps(feature, sort_keys=True, separators=(',', ':')) for feature in sorted(features, key=_sort)]) return ('{"type": "FeatureCollection", ' '"features": [\n' + lines + '\n]}\n') return (_to_collection(features), _to_collection(features_buffered))