def queryFeaturesGeoJSON(self): resJSON = self.queryFeaturesJSON() # Convert Esri JSON results to geojson resGeoJSON = {} for k in resJSON.keys(): # Convert esri JSON to raw, string, geojson rawGeo = arcgis2geojson(resJSON[k]) resGeoJSON[k] = rawGeo return self.combineFeatureCollections(resGeoJSON)
def test_parse_arcgis_multipolygon_with_holes_to_geojson_multipolygon( self): input = { 'type': 'Polygon', 'rings': [[[-100.74462180954974, 39.95017165502381], [-94.50439384003792, 39.91647453608879], [-94.41650267263967, 34.89313438177965], [-100.78856739324887, 34.85708140996771], [-100.74462180954974, 39.95017165502381]], [[-99.68993678392353, 39.341088433448896], [-99.68993678392353, 38.24507658785885], [-98.67919734199646, 37.86444431771113], [-98.06395917020868, 38.210554846669694], [-98.06395917020868, 39.341088433448896], [-99.68993678392353, 39.341088433448896]], [[-96.83349180978595, 37.23732027507514], [-97.31689323047635, 35.967330282988534], [-96.5698183075912, 35.57512048069255], [-95.42724211456674, 36.357601429255965], [-96.83349180978595, 37.23732027507514]], [[-101.4916967324349, 38.24507658785885], [-101.44775114873578, 36.073960493943744], [-103.95263145328033, 36.03843312329154], [-103.68895795108557, 38.03770050767439], [-101.4916967324349, 38.24507658785885]]], 'spatialReference': { 'wkid': 4326 } } output = arcgis2geojson(input) self.assertEqual(output['coordinates'], [[[[-100.74462180954974, 39.95017165502381], [-94.50439384003792, 39.91647453608879], [-94.41650267263967, 34.89313438177965], [-100.78856739324887, 34.85708140996771], [-100.74462180954974, 39.95017165502381]], [[-96.83349180978595, 37.23732027507514], [-97.31689323047635, 35.967330282988534], [-96.5698183075912, 35.57512048069255], [-95.42724211456674, 36.357601429255965], [-96.83349180978595, 37.23732027507514]], [[-99.68993678392353, 39.341088433448896], [-99.68993678392353, 38.24507658785885], [-98.67919734199646, 37.86444431771113], [-98.06395917020868, 38.210554846669694], [-98.06395917020868, 39.341088433448896], [-99.68993678392353, 39.341088433448896]]], [[[-101.4916967324349, 38.24507658785885], [-101.44775114873578, 36.073960493943744], [-103.95263145328033, 36.03843312329154], [-103.68895795108557, 38.03770050767439], [-101.4916967324349, 38.24507658785885]]]]) self.assertEqual(output['type'], 'MultiPolygon')
def test_convert_arcgis_point_to_geojson_point(self): input = { 'x': -66.796875, 'y': 20.0390625, 'spatialReference': { 'wkid': 4326 } } output = arcgis2geojson(input) self.assertEqual(output['coordinates'], [-66.796875, 20.0390625]) self.assertEqual(output['type'], 'Point')
def test_invalid_geometry(self): input = { 'geometry': { 'x': 'NaN', 'y': 'NaN' }, 'attributes': { 'foo': 'bar' } } output = arcgis2geojson(input) self.assertIsNone(output['geometry'])
def arcgisjson_to_geojson(self, geom): """ Takes a list of arcgisjson geometries and converts them to a GeoJSON feature collection. Example below: '{"x":-0.11515950499995142,"y":51.534958948000053,"spatialReference":{"wkid":4326,"latestWkid":4326}}, {"x":-0.11337002699997356,"y":51.536050094000075,"spatialReference":{"wkid":4326,"latestWkid":4326}}' """ payload = json.loads('{"geometries": [' + geom + "]}") features = [] for geometry in payload["geometries"]: features.append({"type": "Feature", "properties": {}, "geometry": arcgis2geojson(geometry)}) feature_collection = {"type": "FeatureCollection", "features": features} return feature_collection
def test_convert_string_json_to_string_json(self): input = json.dumps({ 'x': -66.796875, 'y': 20.0390625, 'spatialReference': { 'wkid': 4326 } }) output = arcgis2geojson(input) self.assertIsInstance(output, str) output = json.loads(output) self.assertEqual(output['coordinates'], [-66.796875, 20.0390625]) self.assertEqual(output['type'], 'Point')
def test_convert_arcgis_polyline_to_geojson_linestring(self): input = { 'paths': [[[6.6796875, 47.8125], [-65.390625, 52.3828125], [-52.3828125, 42.5390625]]], 'spatialReference': { 'wkid': 4326 } } output = arcgis2geojson(input) self.assertEqual(output['coordinates'], [[6.6796875, 47.8125], [-65.390625, 52.3828125], [-52.3828125, 42.5390625]]) self.assertEqual(output['type'], 'LineString')
def test_close_rings_in_convert_arcgis_polygon_to_geojson_polygon(self): input = { 'rings': [[[41.8359375, 71.015625], [56.953125, 33.75], [21.796875, 36.5625]]], 'spatialReference': { 'wkid': 4326 } } output = arcgis2geojson(input) self.assertEqual(output['coordinates'], [[[41.8359375, 71.015625], [56.953125, 33.75], [21.796875, 36.5625], [41.8359375, 71.015625]]]) self.assertEqual(output['type'], 'Polygon')
def test_parse_arcgis_polyline_to_geojson_multilinestring(self): input = { 'paths': [[[41.8359375, 71.015625], [56.953125, 33.75]], [[21.796875, 36.5625], [41.8359375, 71.015625]]], 'spatialReference': { 'wkid': 4326 } } output = arcgis2geojson(input) self.assertEqual(output['coordinates'], [[[41.8359375, 71.015625], [56.953125, 33.75]], [[21.796875, 36.5625], [41.8359375, 71.015625]]]) self.assertEqual(output['type'], 'MultiLineString')
def dx_zoning40(request): # Keep this on one line unless you want to investigate why it doesn't work when on two. url = "https://maps.raleighnc.gov/arcgis/rest/services/Planning/Zoning/MapServer/0/query?outFields=*&outSR=4326&f=json&where=HEIGHT>30 AND ZONE_TYPE='DX-'" payload = {} headers = { "Cookie": 'AGS_ROLES="419jqfa+uOZgYod4xPOQ8Q=="' } response = requests.request("GET", url, headers=headers, data=payload) dx40_zoning_data = arcgis2geojson(response.json()) return render(request, "dx.html", {"dx_zoning_data": dx40_zoning_data})
def test_cli(self): stdout = sys.stdout sys.stdout = io.StringIO() input = u'{ "x": -66.796875, "y": 20.0390625, "spatialReference": { "wkid": 4326 } }' stdin = sys.stdin sys.stdin = io.StringIO(input) main() output = sys.stdout.getvalue().strip() self.assertEqual(output, arcgis2geojson(input)) sys.stdout = stdout sys.stdin = stdin
def ncod(request): # Keep this on one line unless you want to investigate why it doesn't work when on two. url = "https://maps.raleighnc.gov/arcgis/rest/services/Planning/Overlays/MapServer/9/query?where=1%3D1&outFields=*&outSR=4326&f=json" payload = {} headers = { "Cookie": 'AGS_ROLES="419jqfa+uOZgYod4xPOQ8Q=="' } response = requests.request("GET", url, headers=headers, data=payload) ncod_data = arcgis2geojson(response.json()) return render(request, "ncod.html", {"ncod_data": ncod_data})
def test_custom_id_field(self): input = { 'x': -66.796875, 'y': 20.0390625, 'spatialReference': { 'wkid': 4326 }, 'attributes': { 'OBJECTID': 123, 'some_field': 456, } } output = arcgis2geojson(input, 'some_field') self.assertEqual(456, output['id'])
def test_parse_arcgis_multipoint_to_geojson_multipoint(self): input = { 'points': [[[41.8359375, 71.015625], [56.953125, 33.75], [21.796875, 36.5625]]], 'spatialReference': { 'wkid': 4326 } } output = arcgis2geojson(input) self.assertEqual(output['coordinates'], [[ [41.8359375, 71.015625], [56.953125, 33.75], [21.796875, 36.5625], ]]) self.assertEqual(output['type'], 'MultiPoint')
def test_null_id_not_allowed(self): input = { 'x': -66.796875, 'y': 20.0390625, 'spatialReference': { 'wkid': 4326 }, # no 'OBJECTID' or 'FID' in 'attributes' 'attributes': { 'foo': 'bar' } } output = arcgis2geojson(input) self.assertTrue('id' not in output)
def convert_esri_to_geojson_features(geojson_dict: Dict[str, Union[int, float, str, None]]) -> List[dict]: """ Take ESRI GeoJSON response as dict and convert to standard GeoJSON features Args: geojson_dict: ESRI GeoJSON as dict Returns: Standard GeoJSON as list of features """ features = geojson_dict['features'] return [arcgis2geojson(feature) for feature in features]
def test_warning_if_crs_not_4326(self, mock_logging): input = { 'x': 392917.31, 'y': 298521.34, 'spatialReference': { 'wkid': 27700 } } output = arcgis2geojson(input) mock_logging.warning.assert_called_with( "Object converted in non-standard crs - {'wkid': 27700}") self.assertTrue('crs' not in output) self.assertEqual(output['coordinates'], [392917.31, 298521.34])
def test_parse_arcgis_feature_with_custom_id(self): input = { 'geometry': { 'rings': [[[41.8359375, 71.015625], [56.953125, 33.75], [21.796875, 36.5625], [41.8359375, 71.015625]]], 'spatialReference': { 'wkid': 4326 } }, 'attributes': { 'FooID': 123 } } output = arcgis2geojson(input, 'FooID') self.assertEqual(output['id'], 123)
def test_parse_arcgis_feature_with_no_attributes(self): input = { 'geometry': { 'rings': [[[41.8359375, 71.015625], [56.953125, 33.75], [21.796875, 36.5625], [41.8359375, 71.015625]]], 'spatialReference': { 'wkid': 4326 } } } output = arcgis2geojson(input) self.assertEqual(output['geometry']['coordinates'], [[[41.8359375, 71.015625], [56.953125, 33.75], [21.796875, 36.5625], [41.8359375, 71.015625]]]) self.assertEqual(output['geometry']['type'], 'Polygon') self.assertEqual(output['properties'], None)
def test_convert_arcgis_extent_to_geojson_polygon(self): input = { 'xmax': -35.5078125, 'ymax': 41.244772343082076, 'xmin': -13.7109375, 'ymin': 54.36775852406841, 'spatialReference': { 'wkid': 4326 } } output = arcgis2geojson(input) self.assertEqual(output['coordinates'], [[[-35.5078125, 41.244772343082076], [-13.7109375, 41.244772343082076], [-13.7109375, 54.36775852406841], [-35.5078125, 54.36775852406841], [-35.5078125, 41.244772343082076]]])
def test_strip_invalid_rings_in_convert_arcgis_polygon_to_geojson_polygon( self): input = { 'rings': [[[-122.63, 45.52], [-122.57, 45.53], [-122.52, 45.50], [-122.49, 45.48], [-122.64, 45.49], [-122.63, 45.52], [-122.63, 45.52]], [[-83, 35], [-74, 35], [-83, 35]]], 'spatialReference': { 'wkid': 4326 } } output = arcgis2geojson(input) self.assertEqual(output['coordinates'], [[[-122.63, 45.52], [-122.57, 45.53], [-122.52, 45.5], [-122.49, 45.48], [-122.64, 45.49], [-122.63, 45.52], [-122.63, 45.52]]]) self.assertEqual(output['type'], 'Polygon')
def get_data(self): # read in the arcgis fixture dirname = os.path.dirname(os.path.abspath(__file__)) fixture_path = os.path.abspath( os.path.join(dirname, 'fixtures/arcgis.json')) data = json.load(open(fixture_path)) # convert it to geojson features = [] for feature in data['features']: features.append(arcgis2geojson(feature)) data = { "type": "FeatureCollection", "features": features, } data_str = bytes(json.dumps(data), 'utf-8') return (data_str, data)
def save_info(data_name): umi = Umi() umi.login() r = eval('umi.get_' + data_name + '()') r = arcgis2geojson(r) with open('data/' + data_name + '.json', mode='w', encoding='utf-8') as f: f.write(json.dumps(r, ensure_ascii=False)) ''' features = r['features'] for feature in features: f.write(json.dumps(feature, ensure_ascii=False)) f.write('\n') ''' umi.logout()
def test_do_not_modify_original_arcgis_geometry(self): input = { 'geometry': { 'rings': [[[41.8359375, 71.015625], [56.953125, 33.75], [21.796875, 36.5625], [41.8359375, 71.015625]]], 'spatialReference': { 'wkid': 4326 } }, 'attributes': { 'foo': 'bar' } } expected = input output = arcgis2geojson(input) self.assertEqual(input, expected)
def test_parse_arcgis_polygon_to_geojson_multipolygon(self): input = { 'rings': [[[-122.63, 45.52], [-122.57, 45.53], [-122.52, 45.50], [-122.49, 45.48], [-122.64, 45.49], [-122.63, 45.52], [-122.63, 45.52]], [[-83, 35], [-74, 35], [-74, 41], [-83, 41], [-83, 35]]], 'spatialReference': { 'wkid': 4326 } } expected = [[[[-122.63, 45.52], [-122.57, 45.53], [-122.52, 45.5], [-122.49, 45.48], [-122.64, 45.49], [-122.63, 45.52], [-122.63, 45.52]]], [[[-83, 35], [-83, 41], [-74, 41], [-74, 35], [-83, 35]]]] output = arcgis2geojson(input) self.assertEqual(output['coordinates'], expected) self.assertEqual(output['type'], 'MultiPolygon')
def test_id_must_be_string_or_number(self): input = { 'x': -66.796875, 'y': 20.0390625, 'spatialReference': { 'wkid': 4326 }, 'attributes': { 'OBJECTID': 123, 'some_field': { 'not a number': 'or a string' } } } output = arcgis2geojson(input, 'some_field') # 'some_field' isn't a number or string - fall back to OBJECTID self.assertEqual(123, output['id'])
def downloadOne(self, objectids): global _CONTROLLER, _SESSION, _CONTROL_DOWNLOAD try: # print (self.data) objectIds = ', '.join(map(lambda i: str(i), objectids)) self.data['where'] = "{} IN ({})".format(self.oidname, objectIds) response = _SESSION.post(self.urlQuery, self.data, verify=False) response_as_json = json.loads(response.content.decode('utf-8')) if self.data['f'] == 'pjson': response_as_json = arcgis2geojson(response_as_json) # print(response_as_json) if response_as_json.get('error'): raise HTTPError(response_as_json['error']['message']) if self.typeFeature.lower() == 'table': gdf = pd.DataFrame(response_as_json['features']) else: gdf = gpd.GeoDataFrame().from_features(response_as_json) self.responses.append(gdf) _CONTROL_DOWNLOAD = _CONTROL_DOWNLOAD + 1 except (ConnectionError, HTTPError, TimeoutError) as e: print(f'{str(e)}') _CONTROLLER = _CONTROLLER + 1 if _CONTROLLER < _TRIED: print(f"Intento nro {_CONTROLLER}") time.sleep(_WAIT) if _CONTROL_DOWNLOAD == 0: self.data['f'] = 'pjson' self.downloadOne(objectids) else: print("Se supero la cantidad maxima de intentos") except Exception as e: raise RuntimeError(str(e)) finally: _SESSION = requests.session() _CONTROLLER = 0
def test_parse_holes_outside_outer_rings_in_arcgis_polygon_with_holes_to_geojson_polygon( self): input = { 'rings': [[[-122.45, 45.63], [-122.45, 45.68], [-122.39, 45.68], [-122.39, 45.63], [-122.45, 45.63]], [[-122.46, 45.64], [-122.4, 45.64], [-122.4, 45.66], [-122.46, 45.66], [-122.46, 45.64]]], 'spatialReference': { 'wkid': 4326 } } output = arcgis2geojson(input) self.assertEqual( output['coordinates'], [[[-122.45, 45.63], [-122.45, 45.68], [-122.39, 45.68], [-122.39, 45.63], [-122.45, 45.63]], [[-122.46, 45.64], [-122.4, 45.64], [-122.4, 45.66], [-122.46, 45.66], [-122.46, 45.64]]]) self.assertEqual(output['type'], 'Polygon')
def test_convert_arcgis_null_island_to_geojson_point(self): input = {'x': 0, 'y': 0, 'spatialReference': {'wkid': 4326}} output = arcgis2geojson(input) self.assertEqual(output['coordinates'], [0, 0]) self.assertEqual(output['type'], 'Point')
def test_parse_arcgis_feature_with_no_geometry(self): input = {'attributes': {'foo': 'bar'}} output = arcgis2geojson(input) self.assertEqual(output['geometry'], None) self.assertEqual(output['properties']['foo'], 'bar')