x

Re: Abfrage mit Koordinaten: Kartengenerierung nun moeglich!?


Geschrieben von tagtheworld (Gast) am 24. September 2020 17:33:43: [flux]

Als Antwort auf: Abfrage mit Koordinaten: Kartengenerierung nun moeglich!? geschrieben von tagtheworld (Gast) am 28. August 2020 17:21:

hallo und guten Tag fx99 -

vorweg: vielen Dank für Deine Antwort - und den Hinweis. Das ist eine gute Idee. Das könnte ich auch in Python machen.
zu Python gibt es hier guter 'Ideen: https://towardsdatascience.com/loading- … 3882a27fd0

Zitat:

Python and the Overpass API

Now we should have a pretty good grasp of how to query OSM data with the Overpass API, but how can we use this data now? One way to download the data is by using the command line tools curl or wget. In order to do this we need to access one of the Overpass API endpoints, where the one we will look go by the format http://overpass-api.de/api/interpreter?data=query. When using curl we can download the OSM XML of our query by running the command

curl --globoff -o output.xml http://overpass-api.de/api/interpreter?data=node(1);out;

where the previously crafted query comes after data= and the query needs to be urlencoded. The --globoff is important in order to use square and curly brackets without being interpreted by curl. This query returns the following XML result

<?xml version="1.0" encoding="UTF-8"?>
<osm version="0.6" generator="Overpass API 0.7.54.13 ff15392f">
<note>The data included in this document is from www.openstreetmap.org.
The data is made available under ODbL.</note>
<meta osm_base="2018-02-24T21:09:02Z"/>
<node id="1" lat="51.4779481" lon="-0.0014863">
<tag k="historic" v="memorial"/>
<tag k="memorial" v="stone"/>
<tag k="name" v="Prime Meridian of the World"/>
</node>
</osm>

There are various output formats to choose from in the documentation. In order to download the query result as JSON we need to add

[out:json]; to the beginning of our query as in
curl --globoff - o output.json http://overpass-api.de/api/interpreter?data=[out:json];node(1);out;

giving us the previous XML result in JSON format. You can test the query also in the browser by accessing http://overpass-api.de/api/interpreter?data=[out:json];node(1);out;.
But I have promised to use Python to get the resulting query. We can run our well known Biergarten query now with Python by using the requests package in order to access the Overpass API and the json package to read the resulting JSON from the query.

import requests
import json
overpass_url = "http://overpass-api.de/api/interpreter"
overpass_query = """
[out:json];
area["ISO3166-1"="DE"][admin_level=2];
(node["amenity"="biergarten"](area);
way["amenity"="biergarten"](area);
rel["amenity"="biergarten"](area);
);
out center;
"""
response = requests.get(overpass_url,
params={'data': overpass_query})
data = response.json()

In this case we do not have to use urlencoding for our query since this is taken care of by requests.get and now we can store the data or directly use the data further. The data we care about is stored under the elements key. Each element there contains a type key specifying if it is a node, way or relation and an id key. Since we used the out center; statement in our query, we get for each way and relation a center coordinate stored under the center key. In the case of node elements, the coordinates are simply under the lat, lon keys.

import numpy as np
import matplotlib.pyplot as plt

  1. Collect coords into list

coords = []
for element in data['elements']:
if element['type'] == 'node':
lon = element['lon']
lat = element['lat']
coords.append((lon, lat))
elif 'center' in element:
lon = element['center']['lon']
lat = element['center']['lat']
coords.append((lon, lat))

  1. Convert coordinates into numpy array

X = np.array(coords)
plt.plot(X[:, 0], X[:, 1], 'o')
plt.title('Biergarten in Germany')
plt.xlabel('Longitude')
plt.ylabel('Latitude')
plt.axis('equal')
plt.show()

vgl. hier zum Ganzen: https://towardsdatascience.com/loading- … 3882a27fd0

Ich will auf alle Fälle die

a. Daten verwenden - und
b. die Maps ebenso

ein Beispiel:

import csv
import argparse
import sys
import os

def main(args):
@@ -18,7 +19,7 @@ def parse_feature_collection(features, outfile):
# We want to flatten those out

# create the csv writer object
csvwriter = csv.writer(outfile)
csvwriter = csv.writer(outfile, lineterminator=os.linesep)

count = 0
# We'd like to save the first header we see, to maintain the exact same ordering, in case

https://github.com/arnons1/geojson-to-c … 84fd5585bf

by the way: so gings auch

a. mit pandas package.

[python]pandas.read_json()[/python]

um einen JSON string in ein pandas objekt (dataframe etc etx) zu konvertieren.

wenn die Ergebnisse als df gespeichert werdne sollen:

df.to_csv()

Das gibt dann einen string zurück oder auch kann direkt ein a csv-file schreiben.

Dir nochmals vielen Dank!

kann das mit python & auch Pandas machen. btw:
ich über lege ob ich das ggf. auch - also die Daten in eine postgresql reinladen sollte - und das direkt von dem Endpunkt beziehe - via Script...

und nochwas: das ist interessant - die Daten sind sortiert - geographisch nebeneinander liegende "Positionen" - sind offensichtlich nicht unmittelbar in der Datenausgabe untereinander stehend ...

vgl. hier.

@id @type @lon @lat name addr:postcode addr:city addr:street addr:housenumber contact:website contact:email=*
35332685 node -43.9485880 -19.8175998 Hospital Risoleta Tolentino Neves Belo Horizonte Rua das Gabirobas 1
35332689 node -43.9518360 -19.9178800 Prontocor
53254282 node -43.9319894 -19.9260406 Hospital Semper
75600076 node -43.9590472 -19.9505820 Hospital Luxemburgo
78719634 node -43.9528392 -19.9239539 Hospital Vera Cruz Belo Horizonte Avenida Barbacena 653
257565325 node -46.7675910 -1.0538793 Hospital Geral
258373353 node -46.7687408 -1.0502734 Hospital Santo Antônio
258373354 node -46.7639489 -1.0575169 Hospital das Clínicas
264803415 node -71.5444561 -32.9982109 Hospital Naval Almirante Nef Viña del Mar Subida Alessandri S/n
274842924 node -71.5317150 -33.0274915 Clínica Miraflores
274844236 node -71.5227694 -33.0320309 Clinica Los Abetos
302101961 node -62.2772347 -38.7112157 Municipal de Agudos Dr. Leónidas Lucero - Guardia Bravard 37
316377282 node -71.5400129 -33.0281148 Hospital de Niños
316804832 node -62.2724729 -38.7129502 Español - Guardia Casanova 25
316810199 node -62.2491985 -38.7150331 Unidad Sanitaria Delegación Bella Vista Charcas 906
321448936 node -50.5176971 -23.1476022 Hospital Municipal Santa Alice 86350-000 Santa Mariana Rua Manoel da Silva Machado 550
321510735 node -44.0815901 -19.9321119 Posto de Saúde Bernardo Monteiro
330051401 node -70.5573288 -33.5687867 Integramedica Puente Alto Avenida Camilo Henríquez 3296
331107838 node -43.1904033 -22.9367950 Instituto Nacional de Cardiologia
338511118 node -70.5813935 -33.5745464 Hospital de Niños
344023732 node -43.1995077 -22.9530371 Instituto Estadual de Cardiologia Aloysio de Castro
415797968 node -43.1769401 -22.9533746 Hospital Municipal Rocha Maia
416969462 node -43.1788391 -22.9507470 Policlínica de Botafogo 22290-240 Rio de Janeiro Avenida Pasteur 72
428845265 node -65.4048573 -27.0526861 Hospital Parajón Ortíz 4132 Famaillá Belgrano 210
439239039 node -51.5144238 -29.1675219 24h Pronto Atendimento - Hospital Tacchini
440760740 node -51.1942008 -30.0408021 Hospital Petrópolis Rua Coronel Lucas de Oliveira 2040 http://www.hospitalpetropolis.com.br
446894591 node -51.1071545 -29.6887312 Hospital Unimed 93540-290 Novo Hamburgo Rua Waldemar Geib 161
448816561 node -43.1885707 -22.9123555 Instituto Nacional do Câncer (INCa)
450799288 node -43.1906078 -22.9134234 Hospital Espanhol Rio de Janeiro Rua Riachuelo 302
452854210 node -51.8150178 -30.8483932 Hospital Municipal Nossa Senhora Aparecida 96180000 Camaquã Rua Cristóvão Gomes de Andrade 665
453229829 node -44.0470889 -19.9398175 Iria Diniz
453234829 node -44.0441197 -19.9398351 Hospital Santa Helena Contagem - MG Rua Casuarinas 64
453686209 node -44.0259611 -19.9490305 Pronto Socorro Unimed Contagem Avenida Babita Camargos 1695
453690483 node -44.0150767 -19.9654834 Hospital São José
453690501 node -44.0148323 -19.9660607 Hospital Santa Rita
454742102 node -72.5937057 -13.1287556
461805311 node -43.1851671 -22.9330047 Maternidade Escola da UFRJ
475257295 node -62.2352436 -38.7199649 Unidad Sanitaria
476647429 node -51.1982455 -30.0371370 Unidade Álvaro Alvim - HCPA 90420-020 Porto Alegre Rua Professor Álvaro Alvim 400
477364089 node -38.5581389 -3.7446813 Hospital São José Fortaleza Rua Nestor Barbosa 315
492635628 node -47.8084557 -21.1856911 Hospital das Clinicas UE
492636301 node -47.8091569 -21.1852211 Hospital São Francisco
495418452 node -47.4029830 -23.4936943
496267216 node -46.6639769 -23.5094698
496303640 node -47.4269642 -23.5028386 Hospital Psiquiátrico Teixeira Lima
496497349 node -43.3660580 -22.9502944 Casa Santa Ana
501619315 node -35.2048436 -5.8162456 Hospital da UNIMED
505204836 node -48.4829433 -1.4479952 Beneficente Portuguesa
506540736 node -35.2241860 -5.8669350 UMS Cidade Satélite
506577204 node -35.1980430 -5.7816536 Maternidade Escola Januário Cicco 59012-310 Natal - RN Avenida Nilo Peçanha 259
506849960 node -43.1834769 -22.9681155 Hospital Galdino Campos
528921479 node -51.1614296 -29.8391039 São Camilo
530313758 node -53.4052903 -27.4786433 Fundação Hospital Pio XII
548919020 node -43.1919774 -22.9271159 Hospital de Clínicas IV Centenário Rio de Janeiro
559264606 node -76.9788632 -6.0296601 Clínica San Lucas Moyobamba Jr. Alonso de Alvarado 1280
559272981 node -76.9828624 -6.0273680 Lluyllucucha Jirón Dos de Mayo
561522283 node -34.9007258 -8.0511015 Hospital Jaime da Fonte
570751791 node -66.5200994 -33.1397802 Centro de Salud
570758408 node -66.8445685 -32.8779091 Centro de Salud
573601863 node -65.7798270 -28.4719638 IGOM Mota Botello 456
580269641 node -65.7859886 -28.4749041 Hospital Sanitario Interzonal De Niños Eva Peron 4700 San Fernando del Valle de Catamarca
581423130 node -53.4890276 -28.2095797 Hospital Beneficente de Condor 98290-000 Condor Rua Germano Keller
583339444 node -39.7094822 -18.4205525