<?xml version="1.0" encoding="utf-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/">
<channel>
<title>Forum Bauen und Umwelt - Geodatenabfrage aus Google Maps mittels Python</title>
<link>https://bauforum.wirklichewelt.de/</link>
<description>Das Bauforum</description>
<language>de</language>
<item>
<title>Geodatenabfrage aus Google Maps mittels Python</title>
<content:encoded><![CDATA[<p>Während klassische Geodatenbankabfragen recht pingelig sind, wenn es darum geht, einen Ort festzulegen, zeigt Google Maps, wie selbst unscharfe Beschreibungen und Tippfehler zur richtigen Fundstelle führen können.</p>
<p>Netterweise gibt es eine Programmierschnittstelle (API), die die Verwendung dieses Komforts in eigenen Programmen erlaubt. Wer diese Schnittstelle intensiv nutzt, wird lediglich gebeten, einen API-Key zu beantragen, um gegebenenfalls die CPU-Zeit in Rechnung gestellt zu bekommen. Für gelegentliche Nutzung kommt man aber auch ganz gut ohne einen API-Key aus. Man muss nur etwas gegen hin und wieder aufkommende Fehlermeldungen tun, die eine zu intensive Nutzung bemängeln. Eine Sekunde zu warten, reicht oft aus.</p>
<p>Das Python-Modul „geopy“ stellt eine ganze Palette an Funktionen zur Verfügung, um neben der Google-Maps-API auch die Programmierschnittstellen vieler anderer Geodatenanbieter abzufragen. Es gehört leider nicht zum Standardinstallationsumfang von Python 3.6, kann aber schnell mittels „pip install geopy“ oder über die Paketverwaltung besserer Betriebssysteme installiert werden.</p>
<p>Die Klasse <em>geopy.GoogleV3</em> besitzt beispielsweise die Methode <em>geocode</em>, der man nicht mehr als einen ungefähren Ortsnamen („Banohf Bilefelt“) oder eine Beschreibung („höchster Berg in NRW“) übergeben muss, um die exakte Adresse und die Geokoordinaten zu erhalten. Die Klasse <em>geopy.distance.distance</em> berechnet die Entfernung zweier Punkte und berücksichtigt dabei sogar die Erdkrümmung nach verschiedenen Modellen.</p>
<p>Hier ein kleines Python-3.6-Beispielprogramm, das zu einem Ortsnamen oder einer Beschreibung die genaue Adresse heraussucht und gleich noch die nächste Tankstelle, den nächsten Bioladen und den nächsten Aldi-Markt heraussucht <img src="https://bauforum.wirklichewelt.de/images/smilies/smile.png" alt=":-)" /></p>
<pre class="python" style="font-family:monospace;"><ol><li style="background:#f5f5f5;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #800; font-style: italic;">#!/usr/bin/env python3</span></div></li><li style="background:#f9f9f9;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li style="background:#f5f5f5;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #800; font-style: italic;"># Info: https://geopy.readthedocs.io/en/stable/</span></div></li><li style="background:#f9f9f9;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li style="background:#f5f5f5;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #800; font-style: italic;"># Das geopy-Modul gehört nicht zum Standardumfang und</span></div></li><li style="background:#f9f9f9;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #800; font-style: italic;"># muss nachinstalliert werden, z. B. mittels</span></div></li><li style="background:#f5f5f5;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #800; font-style: italic;"># pip install geopy</span></div></li><li style="background:#f9f9f9;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li style="background:#f5f5f5;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #ff7700;">import</span> geopy<span style="color: #000;">,</span> geopy.<span style="color: black;">distance</span></div></li><li style="background:#f9f9f9;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #ff7700;">import</span> <span style="color: #dc143c;">time</span></div></li><li style="background:#f5f5f5;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li style="background:#f9f9f9;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #800; font-style: italic;"># Ohne Schlüssel ist die Zahl der Zugriffe pro Zeitintervall begrenzt.</span></div></li><li style="background:#f5f5f5;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #800; font-style: italic;"># https://developers.google.com/maps/documentation/geolocation/get-api-key</span></div></li><li style="background:#f9f9f9;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">apikey <span style="color: #000;">=</span> <span style="color: #808;">None</span></div></li><li style="background:#f5f5f5;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li style="background:#f9f9f9;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #800; font-style: italic;"># Aufruf der Google-API. Bei Fehlern wegen API-Quota-Überschreitung</span></div></li><li style="background:#f5f5f5;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #800; font-style: italic;"># wird der Versuch nach einer kleinen Wartezeit wiederholt.</span></div></li><li style="background:#f9f9f9;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #ff7700;">def</span> GoSlow<span style="color: black;">&#40;</span>s<span style="color: black;">&#41;</span>:</div></li><li style="background:#f5f5f5;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    antwort <span style="color: #000;">=</span> <span style="color: #080;">&quot;&quot;</span></div></li><li style="background:#f9f9f9;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #ff7700;">while</span> antwort <span style="color: #000;">==</span> <span style="color: #080;">&quot;&quot;</span>:</div></li><li style="background:#f5f5f5;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">        <span style="color: #ff7700;">try</span>:</div></li><li style="background:#f9f9f9;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">            antwort <span style="color: #000;">=</span> geopy.<span style="color: black;">GoogleV3</span><span style="color: black;">&#40;</span>apikey<span style="color: black;">&#41;</span>.<span style="color: black;">geocode</span><span style="color: black;">&#40;</span>s<span style="color: black;">&#41;</span></div></li><li style="background:#f5f5f5;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">        <span style="color: #ff7700;">except</span> geopy.<span style="color: black;">exc</span>.<span style="color: black;">GeocoderQuotaExceeded</span>:</div></li><li style="background:#f9f9f9;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">            <span style="color: #dc143c;">time</span>.<span style="color: black;">sleep</span><span style="color: black;">&#40;</span><span style="color: #008;">1</span><span style="color: black;">&#41;</span></div></li><li style="background:#f5f5f5;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #ff7700;">return</span> antwort</div></li><li style="background:#f9f9f9;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li style="background:#f5f5f5;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">o1 <span style="color: #000;">=</span> <span style="color: #808;">None</span></div></li><li style="background:#f9f9f9;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #ff7700;">while</span> <span style="color: #ff7700;">not</span> o1:</div></li><li style="background:#f5f5f5;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    o1 <span style="color: #000;">=</span> GoSlow<span style="color: black;">&#40;</span><span style="color: #808;">input</span><span style="color: black;">&#40;</span><span style="color: #080;">&quot;Gib einen Ortsnamen oder eine Beschreibung ein: &quot;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></div></li><li style="background:#f9f9f9;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">    <span style="color: #ff7700;">if</span> <span style="color: #ff7700;">not</span> o1:</div></li><li style="background:#f5f5f5;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">        <span style="color: #808;">print</span><span style="color: black;">&#40;</span><span style="color: #080;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>Diesen Ort kennt Google nicht.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: black;">&#41;</span></div></li><li style="background:#f9f9f9;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li style="background:#f5f5f5;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #808;">print</span><span style="color: black;">&#40;</span><span style="color: #080;">&quot;Die Adresse lautet:&quot;</span><span style="color: #000;">,</span>o1.<span style="color: black;">address</span><span style="color: #000;">,</span></div></li><li style="background:#f9f9f9;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">      f<span style="color: #080;">&quot;<span style="color: #000099; font-weight: bold;">\n</span> ({o1.latitude:.6f}, {o1.longitude:.6f})&quot;</span><span style="color: black;">&#41;</span></div></li><li style="background:#f5f5f5;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li style="background:#f9f9f9;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">o2 <span style="color: #000;">=</span> GoSlow<span style="color: black;">&#40;</span><span style="color: #080;">&quot;Aldi nahe &quot;</span>+o1.<span style="color: black;">address</span><span style="color: black;">&#41;</span></div></li><li style="background:#f5f5f5;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">d <span style="color: #000;">=</span> geopy.<span style="color: black;">distance</span>.<span style="color: black;">distance</span><span style="color: black;">&#40;</span>o1.<span style="color: black;">point</span><span style="color: #000;">,</span> o2.<span style="color: black;">point</span><span style="color: black;">&#41;</span></div></li><li style="background:#f9f9f9;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li style="background:#f5f5f5;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #808;">print</span><span style="color: black;">&#40;</span><span style="color: #080;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>Der nächste Aldi-Markt ist&quot;</span><span style="color: #000;">,</span><span style="color: #808;">round</span><span style="color: black;">&#40;</span>d.<span style="color: black;">m</span><span style="color: black;">&#41;</span><span style="color: #000;">,</span></div></li><li style="background:#f9f9f9;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">      <span style="color: #080;">&quot;Meter entfernt:<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #000;">,</span>o2.<span style="color: black;">address</span><span style="color: #000;">,</span></div></li><li style="background:#f5f5f5;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">      f<span style="color: #080;">&quot;<span style="color: #000099; font-weight: bold;">\n</span> ({o2.latitude:.6f}, {o2.longitude:.6f})&quot;</span><span style="color: black;">&#41;</span></div></li><li style="background:#f9f9f9;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li style="background:#f5f5f5;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">o2 <span style="color: #000;">=</span> GoSlow<span style="color: black;">&#40;</span><span style="color: #080;">&quot;Bioladen nahe &quot;</span>+o1.<span style="color: black;">address</span><span style="color: black;">&#41;</span></div></li><li style="background:#f9f9f9;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">d <span style="color: #000;">=</span> geopy.<span style="color: black;">distance</span>.<span style="color: black;">distance</span><span style="color: black;">&#40;</span>o1.<span style="color: black;">point</span><span style="color: #000;">,</span> o2.<span style="color: black;">point</span><span style="color: black;">&#41;</span></div></li><li style="background:#f5f5f5;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li style="background:#f9f9f9;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #808;">print</span><span style="color: black;">&#40;</span><span style="color: #080;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>Der nächste Bioladen ist&quot;</span><span style="color: #000;">,</span><span style="color: #808;">round</span><span style="color: black;">&#40;</span>d.<span style="color: black;">m</span><span style="color: black;">&#41;</span><span style="color: #000;">,</span></div></li><li style="background:#f5f5f5;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">      <span style="color: #080;">&quot;Meter entfernt:<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #000;">,</span>o2.<span style="color: black;">address</span><span style="color: #000;">,</span></div></li><li style="background:#f9f9f9;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">      f<span style="color: #080;">&quot;<span style="color: #000099; font-weight: bold;">\n</span> ({o2.latitude:.6f}, {o2.longitude:.6f})&quot;</span><span style="color: black;">&#41;</span></div></li><li style="background:#f5f5f5;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li style="background:#f9f9f9;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">o2 <span style="color: #000;">=</span> GoSlow<span style="color: black;">&#40;</span><span style="color: #080;">&quot;Tankstelle nahe &quot;</span>+o1.<span style="color: black;">address</span><span style="color: black;">&#41;</span></div></li><li style="background:#f5f5f5;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">d <span style="color: #000;">=</span> geopy.<span style="color: black;">distance</span>.<span style="color: black;">distance</span><span style="color: black;">&#40;</span>o1.<span style="color: black;">point</span><span style="color: #000;">,</span> o2.<span style="color: black;">point</span><span style="color: black;">&#41;</span></div></li><li style="background:#f9f9f9;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li style="background:#f5f5f5;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #808;">print</span><span style="color: black;">&#40;</span><span style="color: #080;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>Die nächste Tankstelle ist&quot;</span><span style="color: #000;">,</span><span style="color: #808;">round</span><span style="color: black;">&#40;</span>d.<span style="color: black;">m</span><span style="color: black;">&#41;</span><span style="color: #000;">,</span></div></li><li style="background:#f9f9f9;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">      <span style="color: #080;">&quot;Meter entfernt:<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #000;">,</span>o2.<span style="color: black;">address</span><span style="color: #000;">,</span></div></li><li style="background:#f5f5f5;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">      f<span style="color: #080;">&quot;<span style="color: #000099; font-weight: bold;">\n</span> ({o2.latitude:.6f}, {o2.longitude:.6f})&quot;</span><span style="color: black;">&#41;</span></div></li></ol></pre><p>Ein paar Beispiele für Programmläufe:</p>
<p>Der höchste Berg in NRW?<br />
<img src="https://bauforum.wirklichewelt.de/images/uploaded/201808311208275b892fbbd6acb.png" alt="[image]"  /></p>
<p>Bahnhof Bielefeld?<br />
<img src="https://bauforum.wirklichewelt.de/images/uploaded/201808311208535b892fd574401.png" alt="[image]"  /></p>
<p>Internationales Geothermiezentrum an der Hochschule Bochum?<br />
<img src="https://bauforum.wirklichewelt.de/images/uploaded/201808311209335b892ffdd71ae.png" alt="[image]"  /></p>
<p>Weitere Informationen zu geopy gibt es auf der Website des Projektes: <a href="https://geopy.readthedocs.io/" target="_blank">https://geopy.readthedocs.io/</a></p>
]]></content:encoded>
<link>https://bauforum.wirklichewelt.de/index.php?id=11659</link>
<guid>https://bauforum.wirklichewelt.de/index.php?id=11659</guid>
<pubDate>Fri, 31 Aug 2018 12:17:32 +0000</pubDate>
<category>Software</category><dc:creator>Martin Vogel</dc:creator>
</item>
</channel>
</rss>
