Note this Question is a continuation of this:
Converting JSON to QGIS GeoJSON: while having multiple features and different types
After some help I managed to convert some JSON to GeoJSON that should be readable in QGIS. The thing is the functions are made I still need to save the files to test them.
The thing is While I know how to save JSON files even making a special function for it, I do not how to save this JSON files altered, currently I am saving these JSON files unaltered but now I want to save them altered.
import requests
import json
import os
import glob
import shutil
# response = requests.get("http://api.gipod.vlaanderen.be/ws/v1/workassignment", params = {"CRS": "Lambert72"})
# text = json.dumps(response.json(),sort_keys=True, indent=4)
# print(text)
# f = open("text.json", "wt")
# f.write(text)
# print(response.status_code)
def process_location_data(location_json):
"""Converts the data point into required geojson format"""
# assigning variable to store geometry details
geometery_details = location_json.get("location").get("geometery")
# geometery_details.pop("crs") # removes the "crs" from geometry
# includes city and location_coordinates
location_details = {
"cities": location_json.get("location").get("cities"),
"coordinate": location_json.get("location").get("coordinate")
}
# EndDateTime
end_datetime = location_json.get("EndDateTime")
# StarDateTime
start_datetime = location_json.get("StarDateTime")
# State
state = location_json.get("State")
# gipodId
gipod_id = location_json.get("gipodId")
# adding all these details into another dict
properties = {
"gipodId": gipod_id,
"StartDateTime": start_datetime,
"EndDateTime": end_datetime,
"state": state,
"location": location_details
}
# creating the final dict to be returned.
geojson_data_point = {
"type": "Feature",
"geometry": geometery_details,
"properties": properties
}
return geojson_data_point
def process_all_location_data(all_location_points):
"""
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)
)
return feature_collection
def fetch_details(url: str, file_name: str):
# Makes request call to get the data of detail
response = requests.get(url)
folder_path = 'api_request_jsons/fetch_details/JSON unfiltered'
text = json.dumps(response.json(), sort_keys=False, indent=4)
print(text)
save_file(folder_path, file_name, text)
return response.json()
# save_file(folder_path,GipodId,text2)
# any other processe
def fetch_points(url: str):
response = requests.get(url)
folder_path = 'api_request_jsons/fetch_points'
text = json.dumps(response.json(), sort_keys=False, indent=4)
print("Points Fetched, going to next step")
i = 1
for obj in response.json():
# Printing object id for confirmation
print("fetching object detais from gipodId " + str(obj.get("gipodId")))
file_name = str(obj.get("gipodId"))
# Getting the object details
all_location_points = [fetch_details(obj.get("detail"), file_name)]
save_file(folder_path, 'points', text)
feature_collection_json = process_all_location_data(all_location_points)
return feature_collection_json
print(feature_collection_json)
# text2 = text[int('coordinate')]
# print(text2)
def save_file(save_path: str, file_name: str, file_information: str):
completeName = os.path.join(save_path, file_name + ".json")
print(completeName)
file1 = open(completeName, "wt")
file1.write(file_information)
file1.close()
api_response_url = "http://api.gipod.vlaanderen.be/ws/v1/workassignment"
fetch_points(api_response_url)
So how do I do this with save_file function I made?
I have kind of found the answer to my question I am posting the code here below. It was kind of obvious how I should have done it but it was kind of mess to see where I have should put my save command.
import requests
import json
import os
import glob
import shutil
def process_location_data(location_json):
"""Converts the data point into required geojson format"""
# assigning variable to store geometry details
geometery_details = location_json.get("location").get("geometery")
#geometery_details.pop("crs") # removes the "crs" from geometry
# includes city and location_coordinates
location_details = {
"cities": location_json.get("location").get("cities"),
"coordinate": location_json.get("location").get("coordinate")
}
#EndDateTime
end_datetime = location_json.get("EndDateTime")
#StarDateTime
start_datetime = location_json.get("StarDateTime")
#State
state = location_json.get("State")
#gipodId
gipod_id = location_json.get("gipodId")
#adding all these details into another dict
properties = {
"gipodId": gipod_id,
"StartDateTime": start_datetime,
"EndDateTime": end_datetime,
"state": state,
"location": location_details
}
# creating the final dict to be returned.
geojson_data_point = {
"type": "Feature",
"geometry" : geometery_details,
"properties": properties
}
return geojson_data_point
def process_all_location_data(all_location_points):
"""
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)
)
return feature_collection
def fetch_details(url: str, file_name: str):
# Makes request call to get the data of detail
response = requests.get(url)
print("Sending Request for details of gpodId: " + file_name)
folder_path ='api_request_jsons/fetch_details/JSON unfiltered'
text = json.dumps(response.json(),sort_keys=False, indent=4)
print("Details extracted for: "+ file_name)
save_file(folder_path,file_name,text)
return response.json()
# save_file(folder_path,GipodId,text2)
# any other processe
def fetch_points(url: str):
response = requests.get(url)
folder_path ='api_request_jsons/fetch_points'
text = json.dumps(response.json(),sort_keys=False, indent=4)
print("Points Fetched, going to next step: Extracting details")
for obj in response.json():
all_location_points = [fetch_details(obj.get("detail"),str(obj.get("gipodId")))]
save_file(folder_path,'points',text)
feature_collection_json = process_all_location_data(all_location_points)
text2 = json.dumps(process_all_location_data(all_location_points))
folder_path2 = "api_request_jsons/fetch_details/Coordinates"
file_name2 = "Converted"
save_file(folder_path2,file_name2,text2)
return feature_collection_json
def save_file(save_path: str, file_name: str, file_information: str):
completeName = os.path.join(save_path, file_name +".json")
print(completeName + " saved")
file1 = open(completeName, "wt")
file1.write(file_information)
file1.close()
api_response_url = "http://api.gipod.vlaanderen.be/ws/v1/workassignment"
fetch_points(api_response_url)
But now after testing the Converted JSON file in QGIS I have found that these JSON returns as GeoJSON without geometry. I have other problems, but you can find that in the previous topic.