def test_geometry(): geom = geometry( {'type': "LineString", 'arcs': [0, 1]}, topology_arcs, scale, translate ) assert geom['type'] == "LineString" assert len(geom['coordinates']) == 26
def test_geometry(): geom = geometry({ 'type': "LineString", 'arcs': [0, 1] }, topology_arcs, scale, translate) assert geom['type'] == "LineString" assert len(geom['coordinates']) == 26
def topojson_to_geojson(topojson_path, geojson_path): with open(topojson_path, "r") as fh: f = fh.read() topology = json.loads(f) # file can be renamed, the first 'object' is more reliable layername = list(topology["objects"].keys())[0] features = topology["objects"][layername]["geometries"] scale = topology["transform"]["scale"] trans = topology["transform"]["translate"] with open(geojson_path, "w") as dest: fc = {"type": "FeatureCollection", "features": []} for id, tf in enumerate(features): f = {"id": id, "type": "Feature"} f["properties"] = tf["properties"].copy() geommap = geometry(tf, topology["arcs"], scale, trans) geom = asShape(geommap).buffer(0) assert geom.is_valid f["geometry"] = geom.__geo_interface__ fc["features"].append(f) dest.write(json.dumps(fc))
def topo2geo(topoJSON: dict) -> dict: scale = topoJSON["transform"]["scale"] translation = topoJSON["transform"]["translate"] topo_features = topoJSON["objects"]["subunits"]["geometries"] # convert topojson to geojson geoJSON = dict(type="FeatureCollection", features=[]) for k, tfeature in enumerate(topo_features): geo_feature = dict(id=k, type="Feature") geo_feature["properties"] = tfeature["properties"] geo_feature["geometry"] = topojson.geometry(tfeature, topoJSON["arcs"], scale, translation) geoJSON["features"].append(geo_feature) return geoJSON
def top2geo(topoJSON): geometries = topoJSON["objects"]["mpios"]["geometries"] geometries_ant = [] ciudades = [] for mpio in geometries: if mpio["properties"]["dpt"] == "ANTIOQUIA": geometries_ant.append(mpio) ciudades.append(mpio["properties"]["name"]) topoJSON["objects"]["mpios"]["geometries"] = geometries_ant topo_features = topoJSON['objects']["mpios"]['geometries'] scale = topoJSON['transform']['scale'] translation = topoJSON['transform']['translate'] geoJSON = dict(type='FeatureCollection', features=[]) for k, tfeature in enumerate(topo_features): geo_feature = dict(id=k, type="Feature") geo_feature['properties'] = tfeature['properties'] geo_feature['geometry'] = geometry(tfeature, topoJSON['arcs'], scale, translation) geoJSON['features'].append(geo_feature) return geoJSON
from shapely.geometry import asShape topojson_path = sys.argv[1] geojson_path = sys.argv[2] with open(topojson_path, 'r') as fh: f = fh.read() topology = json.loads(f) # file can be renamed, the first 'object' is more reliable layername = topology['objects'].keys()[0] features = topology['objects'][layername]['geometries'] scale = topology['transform']['scale'] trans = topology['transform']['translate'] with open(geojson_path, 'w') as dest: fc = {'type': "FeatureCollection", 'features': []} for id, tf in enumerate(features): f = {'id': id, 'type': "Feature"} f['properties'] = tf['properties'].copy() geommap = geometry(tf, topology['arcs'], scale, trans) geom = asShape(geommap).buffer(0) assert geom.is_valid f['geometry'] = geom.__geo_interface__ fc['features'].append(f) dest.write(json.dumps(fc))
topojson_path = sys.argv[1] geojson_path = sys.argv[2] with open(topojson_path, 'r') as fh: f = fh.read() topology = json.loads(f) # file can be renamed, the first 'object' is more reliable layername = topology['objects'].keys()[0] features = topology['objects'][layername]['geometries'] scale = topology['transform']['scale'] trans = topology['transform']['translate'] with open(geojson_path, 'w') as dest: fc = {'type': "FeatureCollection", 'features': []} for id, tf in enumerate(features): f = {'id': id, 'type': "Feature"} f['properties'] = tf['properties'].copy() geommap = geometry(tf, topology['arcs'], scale, trans) geom = asShape(geommap).buffer(0) assert geom.is_valid f['geometry'] = geom.__geo_interface__ fc['features'].append(f) dest.write(json.dumps(fc))
topoJSON = json.loads(jdata) print(topoJSON.keys()) print(topoJSON['objects'].keys()) # dict_keys(['CHN_adm1']) topo_features = topoJSON['objects']['geometries'] scale = topoJSON['transform']['scale'] translation = topoJSON['transform']['translate'] geoJSON = dict(type='FeatureCollection', features=[]) for k, tfeature in enumerate(topo_features): geo_feature = dict(id=k, type="Feature") geo_feature['properties'] = tfeature['properties'] geo_feature['geometry'] = topojson.geometry(tfeature, topoJSON['arcs'], scale, translation) geoJSON['features'].append(geo_feature) print(geoJSON.keys()) print(len(geoJSON['features'])) # geoJSON['features'][0].keys() # geoJSON['features'][0]['geometry'].keys() pts = [] # list of points defining boundaries of polygons for feature in geoJSON['features']: if feature['geometry']['type'] == 'Polygon': pts.extend(feature['geometry']['coordinates'][0]) pts.append([None, None]) # mark the end of a polygon elif feature['geometry']['type'] == 'MultiPolygon': for polyg in feature['geometry']['coordinates']:
# lfs github files apiURL = 'https://media.githubusercontent.com/media/wmgeolab/geoBoundaries/main/releaseData/gbOpen/{iso}/{lvl}/geoBoundaries-{iso}-{lvl}.topojson'.format( iso=iso, lvl=lvl) print('LFS', apiURL) row['apiURL'] = apiURL resp = urllib.request.urlopen(apiURL) topo = json.loads(resp.read()) lyr = list(topo['objects'].keys())[0] print('serializing') objects = topo['objects'][lyr]['geometries'] arcs = topo['arcs'] transform = topo['transform'] features = [] for obj in objects: try: geoj = geometry(obj, arcs, **transform) features.append({'type': 'Feature', 'geometry': geoj}) except Exception as err: print( 'ERROR! Excluding topojson object from spatial stats (could not convert to geojson):', err) continue print('calculating stats') stats = iotools.calc_stats(features) row.update(stats) # write ro row gbWriter.writerow(row) #Add in csv entries based on the github geoBoundaries (Humanitarian) metadata file rfob = urllib.request.urlopen( 'https://raw.githubusercontent.com/wmgeolab/geoBoundaries/main/releaseData/geoBoundariesHumanitarian-meta.csv'