Python Forum
getting back both: the map and the data from a request via overpass-turbo.eu
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
getting back both: the map and the data from a request via overpass-turbo.eu
#1
for a little (!) project i need to have the data (in DB ) and the maps-

this request delivers the Map

[timeout:600]; area["ISO3166-1"="BR"]->.brazil; area["ISO3166-1"="AR"]->.argentina; area["ISO3166-1"="VE"]->.venezuela; area["ISO3166-1"="PE"]->.peru; area["ISO3166-1"="CL"]->.chile; /* more areas..*/ ( nwr[amenity=hospital](area.brazil); nwr[amenity=hospital](area.argentina); nwr[amenity=hospital](area.venezuela); nwr[amenity=hospital](area.peru); nwr[amenity=hospital](area.chile); /* other queries... */ ); out center;
... and this request delivers the data;

[out:csv(::id,::type,::lon, ::lat, "name","addr:postcode","addr:city","addr:street","addr:housenumber","contact:website"," contact:email=*")][timeout:600]; area["ISO3166-1"="BR"]->.brazil; area["ISO3166-1"="AR"]->.argentina; area["ISO3166-1"="VE"]->.venezuela; area["ISO3166-1"="PE"]->.peru; area["ISO3166-1"="CL"]->.chile; /* more areas..*/ ( nwr[amenity=hospital](area.brazil); nwr[amenity=hospital](area.argentina); nwr[amenity=hospital](area.venezuela); nwr[amenity=hospital](area.peru); nwr[amenity=hospital](area.chile); /* other queries... */ ); out center;
i want to have both - the map and the data. But how to achive this. which request gives back both ?






the issue:

we can fetch the data and finally export in xml or Geo Json

from xml to CSV
from GeoJson CSV




If we take a JSON file like the following we can go and convert to a CSV file - also with Python

tried like so:
import json import csv f = open('data.json') data = json.load(f) f.close() f = open('data.csv') csv_file = csv.writer(f) for item in data: csv_file.writerow(item) f.close() However, it did not work. I am using Django and the error I received is: `file' object has no attribute 'writerow'` I then tried the following: import json import csv f = open('data.json') data = json.load(f) f.close() f = open('data.csv') csv_file = csv.writer(f) for item in data: f.writerow(item) # ← changed f.close() I then get the error: `sequence expected` Sample json file: [{ "pk": 22, "model": "auth.permission", "fields": { "codename": "add_logentry", "name": "Can add log entry", "content_type": 8 } }, { "pk": 23, "model": "auth.permission", "fields": { "codename": "change_logentry", "name": "Can change log entry", "content_type": 8 } }, { "pk": 24, "model": "auth.permission", "fields": { "codename": "delete_logentry", "name": "Can delete log entry", "content_type": 8 } }, { "pk": 4, "model": "auth.permission", "fields": { "codename": "add_group", "name": "Can add group", "content_type": 2 } }, { "pk": 10, "model": "auth.permission",
But we can do this like so:

JSON has nested objects, so it normally cannot be directly converted to CSV.
we gotta have to change that to something like this:


{ "pk": 22, "model": "auth.permission", "codename": "add_logentry", "content_type": 8, "name": "Can add log entry" }, ......] Here is my code to generate CSV from that: import csv import json x = """[ { "pk": 22, "model": "auth.permission", "fields": { "codename": "add_logentry", "name": "Can add log entry", "content_type": 8 } }, { "pk": 23, "model": "auth.permission", "fields": { "codename": "change_logentry", "name": "Can change log entry", "content_type": 8 } }, { "pk": 24, "model": "auth.permission", "fields": { "codename": "delete_logentry", "name": "Can delete log entry", "content_type": 8 } } ]""" x = json.loads(x) f = csv.writer(open("test.csv", "wb+")) # Write CSV Header, If you dont need that, remove this line f.writerow(["pk", "model", "codename", "name", "content_type"]) for x in x: f.writerow([x["pk"], x["model"], x["fields"]["codename"], x["fields"]["name"], x["fields"]["content_type"]]) we will get output as: pk,model,codename,name,content_type 22,auth.permission,add_logentry,Can add log entry,8 23,auth.permission,change_logentry,Can change log entry,8 24,auth.permission,delete_logentry,Can delete log entry,8
by the way: i guess that we can do also like so:


a. install the pandas package.
running the following command to install the pandas package under Windows:


With the pandas library, this is as easy - we can go and use two commands

pandas.read_json()
To convert a JSON string to a pandas object (either a series or dataframe).


Then, assuming the results were stored as df:

df.to_csv()
Which can either return a string or write directly to a csv-file.


Based on the verbosity of previous answers, we should all thank pandas for the shortcut.

what do you think!?
Wordpress - super toolkits a. http://wpgear.org/ :: und b. https://github.com/miziomon/awesome-wordpress :: Awesome WordPress: A curated list of amazingly awesome WordPress resources and awesome python things https://github.com/vinta/awesome-python
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Unable to request image from FORM Data usman 0 1,765 Aug-18-2022, 06:23 PM
Last Post: usman
  Write and read back data Aggie64 6 4,039 Apr-18-2022, 03:23 PM
Last Post: bowlofred
  Strategy on updating edits back to data table and object variables hammer 0 1,977 Dec-11-2021, 02:58 PM
Last Post: hammer
  how can I correct the Bad Request error on my curl request tomtom 8 8,697 Oct-03-2021, 06:32 AM
Last Post: tomtom
  Fetching data from multiple tables in a single request. swaroop 0 2,873 Jan-09-2021, 04:23 PM
Last Post: swaroop
  ImportError: cannot import name 'Request' from 'request' abhishek81py 1 6,078 Jun-18-2020, 08:07 AM
Last Post: buran
  PyTest >> Yaml parsed data to create api test request AmR 0 2,813 Apr-14-2020, 11:41 AM
Last Post: AmR
  Download data from webpage after POST request AlDe 0 2,990 Feb-02-2019, 06:26 AM
Last Post: AlDe
  how to retrieve datas with Overpass API python wrapper apollo 0 3,907 Oct-15-2017, 02:27 PM
Last Post: apollo

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.