SlideShare una empresa de Scribd logo
1 de 12
Geographisches Institut / Abteilung Geomatik 
Geocodierung mit RStudio 
am Beispiel Google Maps API 
Thomas Mönkemeier, B.Eng. 
Prof. Dr. Tobia Lakes 
Humboldt-Universität zu Berlin 
moenkemt@geo.hu-berlin.de
Geographisches Institut / Abteilung Geomatik 
Aufgabe: Geocodierung 
• Häufige Anforderung aus der Praxis am Institut: 
„Liste mit Adressen in ein Shape umwandeln“ 
• Gegeben: Liste mit Adressen 
• Gewünscht: Shape für irgendeine GIS-Software 
• Verwendung von RStudio mit der Google Maps API 
als kleines Beispiel für Flexibilität von R
Geographisches Institut / Abteilung Geomatik 
Umgebung: RStudio 
• R ist eine freie Programmiersprache für statistisches Rechnen 
(z.B. Regressionsanalyse, ANOVA...) und statistische Grafiken 
• R gilt zunehmend als Standardsprache für statistische 
Problemstellungen 
• R ist open source und Teil des GNU-Projekts 
• Kommandozeilenkonsole führt Befehle direkt aus 
• RStudio wohl die beliebteste Entwicklungsumgebung 
• Packages für R stellen weitere Funktionen bereit
Geographisches Institut / Abteilung Geomatik 
Beispiel: Google Maps API 
• Aufgabe: Geocodierung 
• Vorteil: Toleranz bei Eingabefehlern 
• Webservice, ohne Registrierung verfügbar 
• Kein API-Schlüssel mehr erforderlich 
• Lizenzbedingungen: Maximal 2500 Anfragen pro Tag, 
Ergebnisse müssen auf einer GoogleMap angezeigt werden 
• Beispiel übertragbar auf andere Geocoder 
wie OpenCage, Nominatim, BingMaps...
Geographisches Institut / Abteilung Geomatik 
Funktion für Google Maps API 
library(RCurl) 
library(RJSONIO) 
googleurl=function(address, format = "json", sensor 
= "false") { 
root="http://maps.google.com/maps/api/geocode/" 
url=paste(root, format, "?address=", address, 
"&sensor=", sensor, sep = "") 
return(URLencode(url)) 
}
Geographisches Institut / Abteilung Geomatik 
Liste der Adressen einlesen 
• Komplette Adressen in erster Spalte (für Google Maps) 
• Optional: Weitere Attribute CSV-getrennt 
• Adress-Liste einlesen in R-Studio Dataframe: 
liste=read.csv("adressen.txt", header=FALSE, 
sep=";", fileEncoding="UTF-8") 
zeilen=nrow(liste) 
spalten=ncol(liste)
Geographisches Institut / Abteilung Geomatik 
Old School: Schleife 
for(i in 1:zeilen) { 
url=googleurl(liste$V1[i]) 
doc=getURL(url) 
coords=fromJSON(doc,simplify = FALSE) 
if(coords$status=="OK") { 
liste$x[i]=coords$results[[1]]$geometry$location$lng 
liste$y[i]=coords$results[[1]]$geometry$location$lat 
liste$type[i]=coords$results[[1]]$geometry$location_type 
} 
Sys.sleep(1) 
}
Geographisches Institut / Abteilung Geomatik 
Punkte aus Liste erzeugen 
• Spatial Data Packages für R einbinden: 
library(sp) 
library(rgdal) 
library(raster) 
• Spatial Dataframe erzeugen aus Liste: 
shapefile = SpatialPointsDataFrame 
(liste[(spalten+1):(spalten+2)], liste) 
• Räumliches Bezugssystem festlegen: 
projection(shapefile)= 
CRS("+proj=longlat +datum=WGS84")
Geographisches Institut / Abteilung Geomatik 
Optional: Höhenangaben 
• SRTM-Daten für die Höhe bei Bedarf vorher ergänzen 
• Raster-Package hält geographische Daten vor: 
srtm=getData('alt',country="DEU") 
• Spalte für Höhe anbinden: 
liste=cbind(liste,ele= 
extract(srtm,liste[(spalten+1):(spalten+2)], 
method="bilinear"))
Geographisches Institut / Abteilung Geomatik 
Punkte als Shape speichern 
• Spatial Dataframe zur Kontrolle plotten: 
plot(shapefile) 
• Und schließlich abspeichern: 
writeOGR(shapefile, dsn = '.', 
layer ='adressen', 
driver = 'ESRI Shapefile') 
• Fertig!
Geographisches Institut / Abteilung Geomatik 
Selbst ausprobieren 
• Download R (z.B. für Windows): 
http://cran.rstudio.com/bin/windows/base/ 
• Download RStudio Desktop Open Source Edition: 
http://www.rstudio.com/ 
• Software installieren, Libraries aus Packages holen 
• Adressen in Textdatei vorbereiten 
• Beispiel für erste eigene Schritte verwenden 
• Am Ergebnis erfreuen und weitermachen!
Geographisches Institut / Abteilung Geomatik 
Viel Erfolg! 
Noch Fragen? 
#gdmoen

Más contenido relacionado

Destacado

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 

Destacado (20)

Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 

GISDday 2014 Geocodierung mit R am Beispiel Google Maps API

  • 1. Geographisches Institut / Abteilung Geomatik Geocodierung mit RStudio am Beispiel Google Maps API Thomas Mönkemeier, B.Eng. Prof. Dr. Tobia Lakes Humboldt-Universität zu Berlin moenkemt@geo.hu-berlin.de
  • 2. Geographisches Institut / Abteilung Geomatik Aufgabe: Geocodierung • Häufige Anforderung aus der Praxis am Institut: „Liste mit Adressen in ein Shape umwandeln“ • Gegeben: Liste mit Adressen • Gewünscht: Shape für irgendeine GIS-Software • Verwendung von RStudio mit der Google Maps API als kleines Beispiel für Flexibilität von R
  • 3. Geographisches Institut / Abteilung Geomatik Umgebung: RStudio • R ist eine freie Programmiersprache für statistisches Rechnen (z.B. Regressionsanalyse, ANOVA...) und statistische Grafiken • R gilt zunehmend als Standardsprache für statistische Problemstellungen • R ist open source und Teil des GNU-Projekts • Kommandozeilenkonsole führt Befehle direkt aus • RStudio wohl die beliebteste Entwicklungsumgebung • Packages für R stellen weitere Funktionen bereit
  • 4. Geographisches Institut / Abteilung Geomatik Beispiel: Google Maps API • Aufgabe: Geocodierung • Vorteil: Toleranz bei Eingabefehlern • Webservice, ohne Registrierung verfügbar • Kein API-Schlüssel mehr erforderlich • Lizenzbedingungen: Maximal 2500 Anfragen pro Tag, Ergebnisse müssen auf einer GoogleMap angezeigt werden • Beispiel übertragbar auf andere Geocoder wie OpenCage, Nominatim, BingMaps...
  • 5. Geographisches Institut / Abteilung Geomatik Funktion für Google Maps API library(RCurl) library(RJSONIO) googleurl=function(address, format = "json", sensor = "false") { root="http://maps.google.com/maps/api/geocode/" url=paste(root, format, "?address=", address, "&sensor=", sensor, sep = "") return(URLencode(url)) }
  • 6. Geographisches Institut / Abteilung Geomatik Liste der Adressen einlesen • Komplette Adressen in erster Spalte (für Google Maps) • Optional: Weitere Attribute CSV-getrennt • Adress-Liste einlesen in R-Studio Dataframe: liste=read.csv("adressen.txt", header=FALSE, sep=";", fileEncoding="UTF-8") zeilen=nrow(liste) spalten=ncol(liste)
  • 7. Geographisches Institut / Abteilung Geomatik Old School: Schleife for(i in 1:zeilen) { url=googleurl(liste$V1[i]) doc=getURL(url) coords=fromJSON(doc,simplify = FALSE) if(coords$status=="OK") { liste$x[i]=coords$results[[1]]$geometry$location$lng liste$y[i]=coords$results[[1]]$geometry$location$lat liste$type[i]=coords$results[[1]]$geometry$location_type } Sys.sleep(1) }
  • 8. Geographisches Institut / Abteilung Geomatik Punkte aus Liste erzeugen • Spatial Data Packages für R einbinden: library(sp) library(rgdal) library(raster) • Spatial Dataframe erzeugen aus Liste: shapefile = SpatialPointsDataFrame (liste[(spalten+1):(spalten+2)], liste) • Räumliches Bezugssystem festlegen: projection(shapefile)= CRS("+proj=longlat +datum=WGS84")
  • 9. Geographisches Institut / Abteilung Geomatik Optional: Höhenangaben • SRTM-Daten für die Höhe bei Bedarf vorher ergänzen • Raster-Package hält geographische Daten vor: srtm=getData('alt',country="DEU") • Spalte für Höhe anbinden: liste=cbind(liste,ele= extract(srtm,liste[(spalten+1):(spalten+2)], method="bilinear"))
  • 10. Geographisches Institut / Abteilung Geomatik Punkte als Shape speichern • Spatial Dataframe zur Kontrolle plotten: plot(shapefile) • Und schließlich abspeichern: writeOGR(shapefile, dsn = '.', layer ='adressen', driver = 'ESRI Shapefile') • Fertig!
  • 11. Geographisches Institut / Abteilung Geomatik Selbst ausprobieren • Download R (z.B. für Windows): http://cran.rstudio.com/bin/windows/base/ • Download RStudio Desktop Open Source Edition: http://www.rstudio.com/ • Software installieren, Libraries aus Packages holen • Adressen in Textdatei vorbereiten • Beispiel für erste eigene Schritte verwenden • Am Ergebnis erfreuen und weitermachen!
  • 12. Geographisches Institut / Abteilung Geomatik Viel Erfolg! Noch Fragen? #gdmoen