skip to Main Content

I’ve downloaded data from this link:
(https://data.cesko.digital/obce/1/obce.json)
Data has been successfully stored in a dictionary and when I try to extract coordinates (‘souradnice’) it acts like NoneType object and doesn’t allow me to do it using for loop. However if I try to print only one coordinate, it gives me no error. Where did I make a mistake?

url = 'https://data.cesko.digital/obce/1/obce.json'
resp = requests.get(url=url)
location_data = resp.json()
type(location_data)
x = list()
y = list()
for z in range(len(cz)):
    y.append(cz[z][0])
    x.append(cz[z][1])

enter image description here

I tried to find the information on forums, tried to convert coordinate data into a np.array but nothing seems to help.

2

Answers


  1. Some municipalities contain None in their "souradnice" key so you have to check for that:

    import requests
    
    url = "https://data.cesko.digital/obce/1/obce.json"
    
    data = requests.get(url).json()
    
    for m in data['municipalities']:
        # some municipalities contain 'null' in "souradnice" key
        # for example "Morkovice-Slížany"
        souradnice = m['souradnice']
        if souradnice is None:
            x, y = None, None
        else:
            x, y = souradnice
        print(m['hezkyNazev'], x, y)
    
    Login or Signup to reply.
  2. You have missing values in your dataset that causes the bug if they are not dealt with.

    x = list()
    y = list()
    
    muncipalites=location_data['municipalities']
    
    for z in range(len(muncipalites)):
        if isinstance(muncipalites[z]['souradnice'],list):
            y.append(muncipalites[z]['souradnice'][0])
            x.append(muncipalites[z]['souradnice'][1])
        else:
            print(f"missing data for {muncipalites[z][list(muncipalites[z].keys())[0]]}")
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search