I'm actually learning how to do some cartography with python but first I would like to convert my json file to a GeoJson dynamically with Python. This is how my Json looks like :
[
{
"shape_name": "unit_shape",
"source_name": "7724C_BUSSY_SAINT_GEORGES_Bussycomore",
"building_id": "7724C",
"chaud_froid": "Chaud",
"geojson": {
"type": "LineString",
"properties": {
"densite_cc": null
},
"coordinates": [
[
2.726142,
48.834359
],
[
2.726149,
48.834367
],
[
2.726202,
48.834422
],
[
2.726262,
48.834429
],
[
2.726307,
48.834434
],
[
2.726316,
48.834435
]
]
},
"to_drop": null,
"id_enquete": null,
"shape_nom": null
},
...(many other features similar to the precedent one)]
Can someone please help me ?
import json
input_file=json.load(open("./data/data.json", "r", encoding="utf-8"))
geojs={
"type": "FeatureCollection",
"features":[
{
"type":"Feature",
"geometry": {
"type":"LineString",
"coordinates":d["geojson"]["coordinates"],
},
"properties":d,
} for d in input_file
]
}
output_file=open("./data/geodata.json", "w", encoding="utf-8")
json.dump(geojs, output_file)
output_file.close()
Related
I was wondering how to perform the following:
1.search for strings in a json and extract their nested components.
given:
"type": "blah",
"animals": [
{
"type": "dog1",
"name": "oscar",
}
},
{
"type": "dog2",
"name": "John",
}
},
{
"type": "cat1",
"name": "Fred",
"Colors": [
"Red"
],
"Contact_info": [
{
"Owner": "Jill",
"Owner_number": "123"
}
],
},
{
"type": "cat3",
"name": "Freddy",
"Colors": [
"Blue"
],
"Contact_info": [
{
"Owner": "Ann",
"Owner_number": "1323"
}
],
From this json, I would like to extract all of the animals that are of type cat like cat1 and cat2, as well as all of the information within that block. Like if I search for cat it should return:
{
"type": "cat1",
"name": "Fred",
"Colors": [
"Red"
],
"Contact_info": [
{
"Owner": "Jill",
"Owner_number": "123"
}
],
},
{
"type": "cat3",
"name": "Freddy",
"Colors": [
"Blue"
],
"Contact_info": [
{
"Owner": "Ann",
"Owner_number": "1323"
}
],
Not necessarily that format, but just all of the information that has type cat. Im trying to search for objects in a json file and extract features from that search as well as anything nested inside of it.
Here is my code so far:
f = open('data.json')
# returns JSON object as
# a dictionary
data = json.load(f)
# Iterating through the json
# list
for i in data:
if i['type'] == 'cat':
print(i['name'])
print(i['colors'])
break
# Closing file
f.close()```
To begin with, I recommend using the with statement that creates a runtime context that allows you to run a group of statements under the control of a context manager.
It’s much more readable and allows you to skip closing the file since the context manager will do everything for you.
Moving to your problem
Suppose your file is called animals.json
# Import json library to work with json files
import json
# Use context manager
with open("animals.json", "rb") as f:
# Load animals list from json file
animals = json.load(f)["animals"]
# Create a list of dictionaries if animal type contains "cat"
cats = [animal for animal in animals if "cat" in animal.get("type")]
# Write data to cats.json
json.dump(cats, open("cats.json", "w"), indent=4, sort_keys=False, ensure_ascii=False)
This code outputs the formatted cats.json file with all necessary elements:
[
{
"type": "cat1",
"name": "Fred",
"Colors": [
"Red"
],
"Contact_info": [
{
"Owner": "Jill",
"Owner_number": "123"
}
]
},
{
"type": "cat3",
"name": "Freddy",
"Colors": [
"Blue"
],
"Contact_info": [
{
"Owner": "Ann",
"Owner_number": "1323"
}
]
}
]
i would like to know why the below posted geojson format is invalid. i tried to visualize its data in
http://geojson.io
but nothing gets displayed.
geojson
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[
[[7.85563468082516,49.90287230375267],[7.855636808249913,49.902782379662085],[7.855776033932631,49.902783753651605],[7.855773906766568,49.902873677746555]]
]
]
},"properties": {"areaOfCoverage":"30"}},
}
Try this:
{
"type": "FeatureCollection",
"features": [
{
"properties":{"areaOfCoverage":"30"},
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
7.85563468082516,
49.90287230375267
],
[
7.855636808249913,
49.902782379662085
],
[
7.855776033932631,
49.902783753651605
],
[
7.85563468082516,
49.90287230375267
]
]
]
}
}
]
}
Suppose I have the following json file. With data1["tenants"][1]['name'] I can select uniquename2. Is there a way to collect the '1' number by looping over the document?
{
"tenants": [{
"key": "identifier",
"name": "uniquename",
"image": "url",
"match": [
"identifier"
],
"tags": [
"tag1",
"tag2"
]
},
{
"key": "identifier",
"name": "uniquename2",
"image": "url",
"match": [
"identifier1",
"identifier2"
],
"tags": ["tag"]
}
]
}
in short: data1["tenants"][1]['name']= uniquename2 data1["tenants"][0]['name'] = uniquename
How can I find out which number has which name. So if I have uniquename2 what number/index corresponds with it?
you can iterate over the tenants to map the index to the name
data = {
"tenants": [{
"key": "identifier",
"name": "uniquename",
"image": "url",
"match": [
"identifier"
],
"tags": [
"tag1",
"tag2"
]
},
{
"key": "identifier",
"name": "uniquename2",
"image": "url",
"match": [
"identifier1",
"identifier2"
],
"tags": ["tag"]
}
]
}
for index, tenant in enumerate(data['tenants']):
print(index, tenant['name'])
OUTPUT
0 uniquename
1 uniquename2
Assuming, you have turned your json into a dictionary already, this is how you can get the index of the firs ocurrence of a name in your list (This relies on the names actually being unique):
data = {
"tenants": [{
"key": "identifier",
"name": "uniquename",
"image": "url",
"match": [
"identifier"
],
"tags": [
"tag1",
"tag2"
]
},
{
"key": "identifier",
"name": "uniquename2",
"image": "url",
"match": [
"identifier1",
"identifier2"
],
"tags": ["tag"]
}
]
}
def index_of(tenants, tenant_name):
try:
return tenants.index(
next(
tenant for tenant in tenants
if tenant["name"] == tenant_name
)
)
except StopIteration:
raise ValueError(
f"tenants list does not have tenant by name {tenant_name}."
)
index_of(data["tenants"], "uniquename") # 0
Im trying to update a json file from XLS file data.
This what I wish to do :
Extract namesFromJson
Extract namesFromXLS
for nameFromXLS in namesFromXLS :
Check if nameFromXLS is in namesFromJson :
if true : then :
extract xls row (of this name)
update jsonFile (of this name)
My problem is when its true, how can I update a jsonfile?
Python code:
import xlrd
import unicodedata
import json
intents_file = open("C:\myJsonFile.json","rU")
json_intents_data = json.load(intents_file)
book = xlrd.open_workbook("C:\myXLSFile.xlsx")
sheet = book.sheet_by_index(0)
row =""
nameXlsValues = []
intentJsonNames =[]
for entity in json_intents_data["intents"]:
intentJsonName = entity["name"]
intentJsonNames.append(intentJsonName)
for row_index in xrange(sheet.nrows):
nameXlsValue = sheet.cell(rowx = row_index,colx=0).value
nameXlsValues.append(nameXlsValue)
if nameXlsValue in intentJsonNames:
#here ,I have to extract row values from xlsFile and update jsonFile
for col_index in xrange(sheet.ncols):
value = sheet.cell(rowx = row_index,colx=col_index).value
if type(value) is unicode:
value = unicodedata.normalize('NFKD',value).encode('ascii','ignore')
row += "{0} - ".format(value)
my json file is like this :
{
"intents": [
{
"id": "id1",
"name": "name1",
"details": {
"tags": [
"tag1"
],
"answers": [
{
"type": "switch",
"cases": [
{
"case": "case1",
"answers": [
{
"tips": [
""
],
"texts": [
"my text to be updated"
]
}
]
},
{
"case": "case2",
"answers": [
{
"tips": [
"tip2"
],
"texts": [
]
}
]
}
]
}
],
"template": "json",
"sentences": [
"sentence1",
" sentence2",
" sentence44"]
}
},
{
"id": "id2",
"name": "name3",
"details": {
"tags": [
"tag2"
],
"answers": [
{
"type": "switch",
"cases": [
{
"case": "case1",
"answers": [
{
"texts": [
""
]
}
]
},
{
"case": "case2",
"answers": [
{
"texts": [
""
]
}
]
}
]
}
],
"sentences": [
"sentence44",
"sentence2"
]
}
}
]
}
My xls file is like this :
[![enter image description here][1]][1]
When you are loading the json data from file into memory it becomes a python dict named 'json_intents_data'.
When the condition 'if nameXlsValue in intentJsonNames' is True you need to update the dict with the data you read from the Excel. (It looks like you know how to do it.)
When the loop 'for row_index in xrange(sheet.nrows):' is done, your dict is updated and you want to save it as a json file.
import json
with open('updated_data.json', 'w') as fp:
json.dump(json_intents_data, fp)
I have geojson file as follows:
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "LineString",
"coordinates": [
[
57.45849609375,
57.36801461845934
],
[
57.10693359375,
56.31044317134597
],
[
59.205322265625,
56.20059291588374
],
[
59.4140625,
57.29091812634045
],
[
57.55737304687501,
57.36801461845934
]
]
}
},
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "LineString",
"coordinates": [
[
59.40307617187499,
57.29685437021898
],
[
60.8203125,
57.314657355733274
],
[
60.74340820312499,
56.26776108757582
],
[
59.227294921875,
56.21281407174654
],
[
59.447021484375,
57.29091812634045
]
]
}
}
]
}
I want to replace LineString in "type": "LineString" with Polygon, and also, replace coordinates last point of each linestring by coordinates of first point to make it close if it has more than 3 points.
How can I do it in Python with geopandas or pandas? Thanks.
Here is expected output:
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [
[
57.45849609375,
57.36801461845934
],
[
57.10693359375,
56.31044317134597
],
[
59.205322265625,
56.20059291588374
],
[
59.4140625,
57.29091812634045
],
[
57.45849609375,
57.36801461845934
]
]
}
},
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [
[
59.40307617187499,
57.29685437021898
],
[
60.8203125,
57.314657355733274
],
[
60.74340820312499,
56.26776108757582
],
[
59.227294921875,
56.21281407174654
],
[
59.40307617187499,
57.29685437021898
]
]
}
}
]
}
Script to get type and coordinates of first LineString:
import json
from pprint import pprint
with open('data.geojson') as f:
data = json.load(f)
pprint(data)
data["features"][0]["geometry"]['type']
data["features"][0]["geometry"]['coordinates']
You can achieve that with the json module:
file_line = 'file.json'
file_poly = 'file_poly.json'
import json
with open(file_line, 'r') as f:
data = json.load(f)
for feature in data['features']:
if (feature['geometry']['type'] == 'LineString') & (len(feature['geometry']['coordinates']) >= 3):
feature['geometry']['type'] = 'Polygon'
feature['geometry']['coordinates'].append(feature['geometry']['coordinates'][0])
with open(file_poly, 'w+') as f:
json.dump(data, f, indent=2)