def setUp(self) -> None: self.feature1 = Feature( id="1", geometry={ "type": "Point", "coordinates": [53.0, -4.0] }, properties={ "title": "Feature 1", "summary": "The first feature", "link": "http://example.org/features/1", }, ) self.feature2 = Feature( id="1", geometry={ "type": "Point", "coordinates": [54.0, -3.0] }, properties={ "title": "Feature 2", "summary": "The second feature", "link": "http://example.org/features/2", }, )
def load_geojson(file_name): with open(file_name, 'r') as fd: parser = ijson.parse(fd) for prefix, event, value in parser: if (prefix, event) == ('features.item', 'start_map'): feature = Feature() elif (prefix, event) == ('features.item', 'end_map'): yield feature if (prefix, event) == ('features.item.properties', 'start_map'): properties = JpCityProperties() elif (prefix, event) == ('features.item.properties', 'end_map'): feature.properties = properties.__dict__ elif (prefix, event, value) == ('features.item.properties', 'map_key', 'A27_005'): properties.a27_005 = parser.next()[2] elif (prefix, event, value) == ('features.item.properties', 'map_key', 'A27_006'): properties.a27_006 = parser.next()[2] elif (prefix, event, value) == ('features.item.properties', 'map_key', 'A27_007'): properties.a27_007 = parser.next()[2] elif (prefix, event, value) == ('features.item.properties', 'map_key', 'A27_008'): properties.a27_008 = parser.next()[2] elif (prefix, event) == ('features.item.geometry', 'start_map'): geometry = MultiPolygon() elif (prefix, event) == ('features.item.geometry.type', 'string'): if value == "MultiPolygon": geometry = MultiPolygon() elif value == "Polygon": geometry = Polygon() else: raise Exception elif (prefix, event) == ('features.item.geometry', 'end_map'): feature.geometry = geometry elif (prefix, event) == ('features.item.geometry.coordinates', 'start_array'): coordinates = [] elif (prefix, event) == ('features.item.geometry.coordinates', 'end_array'): geometry.coordinates = coordinates elif (prefix, event) == ('features.item.geometry.coordinates.item', 'start_array'): coordinates_item = [] elif (prefix, event) == ('features.item.geometry.coordinates.item', 'end_array'): coordinates.append(coordinates_item) elif (prefix, event) == ('features.item.geometry.coordinates.item.item', 'start_array'): if isinstance(geometry, MultiPolygon): coordinates_item_item = [] else: coordinates_item.append((parser.next()[2], parser.next()[2])) elif (prefix, event) == ('features.item.geometry.coordinates.item.item', 'end_array'): if isinstance(geometry, MultiPolygon): coordinates_item.append(coordinates_item_item) elif (prefix, event) == ('features.item.geometry.coordinates.item.item.item', 'start_array'): if isinstance(geometry, MultiPolygon): coordinates_item_item.append((parser.next()[2], parser.next()[2]))
def hexagons_dataframe_to_geojson(df_hex, file_output=None): """ Produce the GeoJSON for a dataframe that has a geometry column in geojson format already, along with the columns hex_id and value Ex counts_by_hexagon(data) """ list_features = [] for i, row in df_hex.iterrows(): feature = Feature(geometry=row["geometry"], id=row["hex_id"], properties={"value": row["value"]}) list_features.append(feature) feat_collection = FeatureCollection(list_features) geojson_result = json.dumps(feat_collection) #optionally write to file if file_output is not None: with open(file_output, "w") as f: json.dump(feat_collection, f) return geojson_result
def testgeojsonmetadata(self): expectedRes = '{"geometry": {"coordinates": [[[0, 55.77657302], [0, 40.97989807], [-11.25, 40.97989807], [-11.25, 55.77657302], [0, 55.77657302]]], "properties": {"app": "OpenCPN", "app:url": "http://opencpn.org/ocpn/", "date": "2016-05-17T20:59:00.000Z", "filesize": 12345, "format": "KAP", "name:de": "Golf von Biskaya", "name:en": "Gulf of Biscay", "url": "ftp://ftp.gwdg.de/pub/misc/openstreetmap/openseamap/chartbundles/kap/OSM-OpenCPN-KAP2-GulfOfBiscay-20160515-1145.7z"}, "type": "Polygon"}, "properties": {}, "type": "Feature"}' NW1 = (12.0, 50.0) SW1 = (12.0, 49.0) SE1 = (13.0, 49.0) NE1 = (13.0, 50.0) [0, 55.77657302], [0, 40.97989807], [-11.25, 40.97989807], [-11.25, 55.77657302], [0, 55.77657302] sample_obj = Polygon( [[(0, 55.77657302), (0, 40.97989807), (-11.25, 40.97989807), (-11.25, 55.77657302), (0, 55.77657302)]], properties={ "name:en": "Gulf of Biscay", "name:de": "Golf von Biskaya", "format": "KAP", "app": "OpenCPN", "app:url": "http://opencpn.org/ocpn/", "url": "https://ftp.gwdg.de/pub/misc/openstreetmap/openseamap/chartbundles/kap/OSM-OpenCPN-KAP2-GulfOfBiscay-20160515-1145.7z", "date": "2016-05-17T20:59:00.000Z", "filesize": 12345 }) sample_obj = Feature(geometry=sample_obj) if sample_obj.is_valid is not True: print(sample_obj.errors()) self.assertEqual(sample_obj.is_valid, True) self.assertEqual(expectedRes, geojson.dumps(sample_obj, sort_keys=True)) pass
def hexagons_dataframe_to_geojson( df_hex, id_field, geometry_field, value_field, file_output=None ): """Produce the GeoJSON representation containing all geometries in a dataframe based on a column in geojson format (geometry_field). Parameters ---------- df_hex : DataFrame, required The dataframe where one row represents a specific geometric shape and a value. id_field : string, required The column name of a column which serves as a unique identifier. geometry_field: string, required The column name of the column containing the geometric shape in geojson format. value_field : string, required The column name of the column containing values that should be appended to the results as properties named "value". """ list_features = [] for _, row in df_hex.iterrows(): feature = Feature( geometry=row[geometry_field], id=row[id_field], properties={"value": row[value_field]}, ) list_features.append(feature) feat_collection = FeatureCollection(list_features) geojson_result = json.dumps(feat_collection) # optionally write to file if file_output is not None: with open(file_output, "w") as f: json.dump(feat_collection, f) return geojson_result
points.append((chart.NE.lon, chart.NE.lat)) points.append((chart.NW.lon, chart.NW.lat)) points.append((chart.SE.lon, chart.SE.lat)) points.append((chart.SW.lon, chart.SW.lat)) else: print("skip chart {}".format(chart.name)) huell = convexhull(points) huell.append(huell[0]) outfilename = archivfilename[:-3] + '.geojson' url = "{}{}/{}".format(options.url, options.InPath, os.path.basename(archivfilename)) # generate geojson object jsonres = Feature(geometry=Polygon([huell]), properties={ "name:en": mapname, "format": "KAP", "app": "OpenCPN", "app:url": 'https://opencpn.org/', "url": url, "date": date, "filesize": filesize }) # print created geojson object to output file with open(outfilename, 'w') as f: f.write(geojson.dumps(jsonres, indent=4, sort_keys=True))