Geographic Information Systems Asked on June 2, 2021
I have some code to convert a JSON object requested from an API to a GeoJSON object based on the standard rfc7946. You see the data requested from the API was not actually a GeoJSON formated so I had to convert it to Code is here below:
def process_location_data(location_json, sper_years : int):
# assigning variable to store geometry details
geometry_details = location_json.get("location").get("geometry")
# includes city and location_coordinates
location_details = {
"cities": location_json.get("location").get("cities"),
"coordinate": location_json.get("location").get("coordinate"),
}
# TODO: You declare the gipod_id var here. What is it used for? -sg
gipod_id = location_json.get("gipodId")
# EndDateTime
end_datetime = location_json.get("endDateTime")
# Sper_date_time
sper_date_time = sper_berekening(location_json.get("endDateTime"), sper_years)
# sper_year_period
sper_year_period = sper_years
# StartDateTime
start_datetime = location_json.get("startDateTime")
# State
state = location_json.get("state")
# type
# NOTE -> Modified "type" to "loc_type". "type" is an internal Python
# keyword, and should
# never be used as a variable. -sg
loc_type = location_json.get("type")
# owner
owner = location_json.get("owner")
# lastestUpdate
lastestUpdate = location_json.get("latestUpdate")
# reference
reference = location_json.get("reference")
# gipodId
gipod_id = location_json.get("gipodId")
# cities
cities = location_json.get("location").get("cities")
# adding all these details into another dict
properties = {
"gipodId": gipod_id,
"StartDate": start_datetime,
"EndDate": end_datetime,
"lastestUpdate": lastestUpdate,
"Sper_year": sper_year_period,
"sperdate": sper_date_time,
"state": state,
"type": loc_type,
"owner": owner,
"reference": reference,
"location": location_details,
}
# creating the final dict to be returned.
geojson_data_point = {
"type": "Feature",
"geometry": geometry_details,
"properties": properties,
}
return geojson_data_point
def process_all_location_data(all_location_points, sper_year: int):
# For all the points in the location details we will
# create the feature collection
feature_collection = {
"type": "FeatureCollection",
"features": [],
} # creates dict with zero features.
for data_point in all_location_points:
feature_collection.get("features").append(process_location_data(data_point, sper_year))
return feature_collection
This can be read in QGIS just fine, the problem is however that the GeoJSON is in WGS84. While in reality it should be Lambert 72 from the start.
Because we requested the data is in Lambert 72 (ESPG:31370), heck the CRS is even referenced as Lambert 72. The coordinates clearly are in Lambert 72. See the JSON object below. We use Lambert 72 as the standard in our projects, so how do I solve this problem to make sure it is Lambert 72, in my Python Script? Yes I know we can technically solve this with GDAL, in fact I kind of already have but I need something in can set in the begining.
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"coordinates": [
[
[
134258.77936085,
183057.08260379
],
[
134244.49435718,
183065.17760501
],
[
134235.8006087,
183043.27259859
],
[
134234.37185962,
183035.53384814
],
[
134231.15811069,
183027.91509661
],
[
134229.49185855,
183019.22385058
],
[
134229.84810931,
183016.84385332
],
[
134227.69435794,
183002.55759844
],
[
134226.88436038,
182986.11134753
],
[
134225.4556113,
182974.68259844
],
[
134219.74186046,
182962.54009661
],
[
134226.82435901,
182954.10509905
],
[
134228.01560886,
182954.10509905
],
[
134229.08685977,
182953.15259966
],
[
134255.59083086,
182909.58959991
],
[
134268.68583208,
182884.35209686
],
[
134279.01832873,
182869.62709839
],
[
134291.8758269,
182857.00709564
],
[
134304.97082812,
182848.19709808
],
[
134318.06582934,
182839.86459381
],
[
134324.25832659,
182834.86459381
],
[
134325.44832903,
182831.2920993
],
[
134321.63833147,
182827.48459656
],
[
134288.32833391,
182792.01960022
],
[
134275.99582964,
182779.49459869
],
[
134245.67332751,
182732.83959991
],
[
134223.15082842,
182690.10209686
],
[
134214.5308333,
182672.69709808
],
[
134184.05583102,
182627.93460114
],
[
134141.54333025,
182592.75709564
],
[
134145.11332995,
182588.47209961
],
[
134187.73333269,
182626.32959778
],
[
134217.32832629,
182670.68460114
],
[
134236.13833147,
182703.78209717
],
[
134249.38833147,
182729.14959747
],
[
134264.38833147,
182752.48459656
],
[
134278.67333514,
182775.33959991
],
[
134301.95832354,
182802.2920993
],
[
134329.57833391,
182830.38710052
],
[
134370.92333514,
182857.30459625
],
[
134430.44832903,
182845.87709839
],
[
134455.20832354,
182872.30459625
],
[
134451.87583452,
182874.20959503
],
[
134428.3058272,
182848.73459656
],
[
134369.73333269,
182860.86459381
],
[
134331.63833147,
182835.86459381
],
[
134304.97082812,
182851.57959778
],
[
134283.88708789,
182869.41334945
],
[
134276.38708789,
182879.88959915
],
[
134264.98833758,
182900.49709732
],
[
134247.22083575,
182934.61584884
],
[
134233.35083682,
182956.91334945
],
[
134231.32708651,
182961.91334945
],
[
134233.58958728,
182967.98459655
],
[
134234.38708789,
182978.96709854
],
[
134235.45833499,
182985.27709991
],
[
134236.88708789,
182986.58585006
],
[
134236.23208529,
183005.64584762
],
[
134238.13708789,
183008.5033496
],
[
134239.92333514,
183021.4795993
],
[
134251.28583438,
183048.63334686
],
[
134258.77936085,
183057.08260379
]
]
],
"type": "Polygon",
"crs": {
"type": "name",
"properties": {
"name": "urn:ogc:def:crs:EPSG::31370"
}
}
},
Edit 17/04/2021: Thanks to the_dark_side I have solved the problem, I had to change this part of the code to this:
def process_all_location_data(all_location_points, sper_year: int):
# For all the points in the location details we will
# create the feature collection
feature_collection = {
"type": "FeatureCollection",
"crs": {
"type": "name",
"properties": {
"name": "urn:ogc:def:crs:EPSG::31370"
}},
"features": [],
} # creates dict with zero features.
for data_point in all_location_points:
feature_collection.get("features").append(process_location_data(data_point, sper_year))
return feature_collection
Your GeoJSON is not formatted correctly. The CRS definition is nested too deep. It should be formatted in the following way, and QGIS will be able to read the CRS info properly. Copy the following into your IDE, save it as a geojson, and it will be read by QGIS.
Make sure to check the formatting of the geojson in Python before you export it (the dict containing your CRS info should be separate from the array of features).
Note: if you export a feature collection, then you might want to create it using following method:
crs = {
"type": "name",
"properties": {
"name": "urn:ogc:def:crs:EPSG::31370"
}
}
myFC =FeatureCollection(features ,crs=crs)
Here, features
is your array of features.
{
"type": "FeatureCollection",
"crs": {
"type": "name",
"properties": {
"name": "urn:ogc:def:crs:EPSG::31370"
}},
"features": [
{
"type": "Feature",
"geometry": {"type": "Polygon",
"coordinates": [
[
[
134258.77936085,
183057.08260379
],
[
134244.49435718,
183065.17760501
],
[
134235.8006087,
183043.27259859
],
[
134234.37185962,
183035.53384814
],
[
134231.15811069,
183027.91509661
],
[
134229.49185855,
183019.22385058
],
[
134229.84810931,
183016.84385332
],
[
134227.69435794,
183002.55759844
],
[
134226.88436038,
182986.11134753
],
[
134225.4556113,
182974.68259844
],
[
134219.74186046,
182962.54009661
],
[
134226.82435901,
182954.10509905
],
[
134228.01560886,
182954.10509905
],
[
134229.08685977,
182953.15259966
],
[
134255.59083086,
182909.58959991
],
[
134268.68583208,
182884.35209686
],
[
134279.01832873,
182869.62709839
],
[
134291.8758269,
182857.00709564
],
[
134304.97082812,
182848.19709808
],
[
134318.06582934,
182839.86459381
],
[
134324.25832659,
182834.86459381
],
[
134325.44832903,
182831.2920993
],
[
134321.63833147,
182827.48459656
],
[
134288.32833391,
182792.01960022
],
[
134275.99582964,
182779.49459869
],
[
134245.67332751,
182732.83959991
],
[
134223.15082842,
182690.10209686
],
[
134214.5308333,
182672.69709808
],
[
134184.05583102,
182627.93460114
],
[
134141.54333025,
182592.75709564
],
[
134145.11332995,
182588.47209961
],
[
134187.73333269,
182626.32959778
],
[
134217.32832629,
182670.68460114
],
[
134236.13833147,
182703.78209717
],
[
134249.38833147,
182729.14959747
],
[
134264.38833147,
182752.48459656
],
[
134278.67333514,
182775.33959991
],
[
134301.95832354,
182802.2920993
],
[
134329.57833391,
182830.38710052
],
[
134370.92333514,
182857.30459625
],
[
134430.44832903,
182845.87709839
],
[
134455.20832354,
182872.30459625
],
[
134451.87583452,
182874.20959503
],
[
134428.3058272,
182848.73459656
],
[
134369.73333269,
182860.86459381
],
[
134331.63833147,
182835.86459381
],
[
134304.97082812,
182851.57959778
],
[
134283.88708789,
182869.41334945
],
[
134276.38708789,
182879.88959915
],
[
134264.98833758,
182900.49709732
],
[
134247.22083575,
182934.61584884
],
[
134233.35083682,
182956.91334945
],
[
134231.32708651,
182961.91334945
],
[
134233.58958728,
182967.98459655
],
[
134234.38708789,
182978.96709854
],
[
134235.45833499,
182985.27709991
],
[
134236.88708789,
182986.58585006
],
[
134236.23208529,
183005.64584762
],
[
134238.13708789,
183008.5033496
],
[
134239.92333514,
183021.4795993
],
[
134251.28583438,
183048.63334686
],
[
134258.77936085,
183057.08260379
]
]
]}
}]
}
Correct answer by the_darkside on June 2, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP