GithubHelp home page GithubHelp logo

rcarto / photon Goto Github PK

View Code? Open in Web Editor NEW
45.0 3.0 8.0 24 KB

:dart: R Interface to the Photon API / Interface entre R et l'API de photon

Home Page: http://rgeomatic.hypotheses.org/?p=622

R 100.00%
photon photon-api r geocoder geocoding

photon's Introduction

Project Status: Abandoned – Initial development has started, but there has not yet been a stable, usable release; the project has been abandoned and the author(s) do not intend on continuing development. The project has been abandoned and the author do not intend on continuing development.

R Interface to the Photon API

Photon is an open source geocoder built for OpenStreetMap data and based on elasticsearch. This package allows to query a photon API and get the results in a data frame.

Photon web site: https://photon.komoot.io/
Photon source code: https://github.com/komoot/photon

Install Instructions

require(devtools)  
devtools::install_github(repo = 'rCarto/photon')  

Usage

geocode()

address <- c("19 rue Michel Bakounine, 29600 Morlaix, France",
             "5 rue Proudhon, 34130 Mauguio France",
             "2 Emma Goldmanweg, Tilburg, Netherlands",
             "36 Strada Panait Israti, Bucarest, Romania")
place <- geocode(address, limit = 1, key = "place")
place
location osm_id osm_type name housenumber street postcode city state country osm_key osm_value lon lat msg
19 rue Michel Bakounine, 29600 Morlaix, France 3241060871 N NA 19 Rue Michel Bakounine 29600 Morlaix Brittany France place house -3.816435 48.59041 NA
5 rue Proudhon, 34130 Mauguio France 3700168030 N NA 5 Rue Proudhon 34130 Mauguio Occitania France place house 4.008024 43.61583 NA
2 Emma Goldmanweg, Tilburg, Netherlands 2844596196 N NA 2 Emma Goldmanweg 5032MN Tilburg North Brabant The Netherlands place house 5.041361 51.53783 NA
36 Strada Panait Israti, Bucarest, Romania 2838254765 N NA 36 Strada Panait Istrati 011547 Bucharest NA Romania place house 26.064266 44.46227 NA
geocode("Montreuil", limit = 1)
location osm_id osm_type name housenumber street postcode city state country osm_key osm_value lon lat msg
Montreuil 2622231 R Montreuil NA NA 62170 NA Nord-Pas-de-Calais and Picardy France place village 1.764228 50.46375 NA
geocode("Montreuil", locbias = c(2.4, 48.9), limit = 1)
location osm_id osm_type name housenumber street postcode city state country osm_key osm_value lon lat msg
Montreuil 129423 R Montreuil NA NA 93100 NA Ile-de-France France place city 2.441218 48.86234 NA
# with a typical local install of photon
place <- geocode(address, limit = 1, key = "place", server = "http://0.0.0.0:2322/")

reverse()

address <- c("19 rue Michel Bakounine, 29600 Morlaix, France",
             "5 rue Proudhon, 34130 Mauguio France",
             "2 Emma Goldmanweg, Tilburg, Netherlands",
             "36 Strada Panait Israti, Bucarest, Romania")
place <- geocode(address, limit = 1, key = "place")
place2 <- reverse(x = place$lon, y = place$lat)
place2
x y osm_id osm_type name housenumber street postcode city state country osm_key osm_value lon lat msg
-3.816435 48.59041 3241060871 N NA 19 Rue Michel Bakounine 29600 Morlaix Brittany France place house -3.816435 48.59041 NA
4.008024 43.61583 3700168030 N NA 5 Rue Proudhon 34130 Mauguio Occitania France place house 4.008024 43.61583 NA
5.041361 51.53783 2844596196 N NA 2 Emma Goldmanweg 5032MN Tilburg North Brabant The Netherlands place house 5.041361 51.53783 NA
26.064266 44.46227 2838254765 N NA 36 Strada Panait Istrati 011547 Bucharest NA Romania place house 26.064266 44.46227 NA
identical(place[,2:12], place2[,3:13])
>TRUE

photon's People

Contributors

jandix avatar rcarto avatar rcura avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

photon's Issues

Geocoding does not work well

Hi @RCura @rCarto ,

I tried to use the package, but it only returns a very few records, the following example addresses cannot be geocoded with your package:

Germany, Augsburg, Berliner Allee 30
Germany, Bautzen, Gesundbrunnenring 23
Germany, Bielefeld, Stadtholz 18
Germany, Bochum, Brandwacht 1
Germany, Bonn, Lievelingsweg 112
Germany, Bottrop, Hans-Sachs-Str 78-80
Germany, Braunschweig, Feuerwehrstr 1
Germany, Bremen, Wandrahm 24
Germany, Bremerhaven, Zur Hexenbrucke 12
Germany, Chemnitz, Schadestr 11
Germany, Cottbus, Dresdener Str 46
Germany, Cuxhaven, Schulstr 3
Germany, Darmstadt, Bismarckstr 86
Germany, Delmenhorst, Rudolf-Koniger-Str 35
Germany, Dessau-Rosslau, Innsbrucker Str 8

You can use the following code to get all the data I am trying to geocode:

####################################################################

require(devtools)
devtools::install_github(repo = 'rCarto/photon')
library(photon)
need <- c("RSelenium", "plyr", "dplyr", "pdftools", "RCurl", "RJSONIO", "nominatim", "stringi")

ins <- installed.packages()[, 1]
(Get <- need[which(is.na(match(need,ins)))])
if(length(Get) > 0) {install.packages(Get)}
eval(parse(text = paste("library(", need, ")")))

####################################################################

Custom cleaning functions

adress.cleaner <- function(text_in) {
text_in <- gsub("[.]", "", text_in)
text_in <- stri_trans_general(text_in, "Latin-ASCII")
z <- strsplit(text_in, split = " ")
z2 <- lapply(z, function(x) {
sapply(x, function(y) {
check_num <- suppressWarnings(as.numeric(y))
if (is.na(check_num) & nchar(gsub(",", "", y)) < 3) {
y <- ""
}
return(y)
})
})

z3 <- unlist(lapply(z2, function(x) {
return(paste(x, collapse = " "))
}))
return(z3)
}

####################################################################

fire_station_pdf <- pdftools::pdf_text("https://s3-eu-central-1.amazonaws.com/de-hrzg-khl/kh-ffe/public/artikel-pdfs/Free_PDF/BF_LISTE_20016.pdf")
fire_station_pdf <- paste0(fire_station_pdf, collapse = " ")

We define the range of the pdf to scrape (first table on page 1 - first line to last line)

pattern1 <- "Aachen\s+Stolberger\s+Str.\s+155\s+52068\s+Aachen\s+0241.43237-9000\s+0241.512527(.)*Herten\s+An\s+der\s+Feuerwache\s+7-9\s+45699\s+Herten\s+02366.31024\s+02366.39258"
extract1 <- regmatches(fire_station_pdf, regexpr(pattern1, fire_station_pdf))
extract1 <- paste0(extract1, "\n ") # need to add this here before joining with the second extract

We look at each line and see that the columns have a fixed width

a <- nchar("Aachen ")
b <- nchar("Stolberger Str. 155 ")
c <- nchar("52068 Aachen ")
d <- nchar("0241.43237-9000 ")

0241.512527\n this part is not really important because we have \n to determine the end of this section

We define the range of the pdf to scrape (third table on page 3 - first line to last line)

pattern2 <- "Hildesheim\s+An\s+der\s+Feuerwache\s+4-7\s+31135\s+Hildesheim\s+05121.3012222\s+05121.12600(.)*Zwickau\s+Crimmitschauer\s+Str.\s+35\s+08056\s+Zwickau\s+0375.83-5700\s+0375.215764"
extract2 <- regmatches(fire_station_pdf, regexpr(pattern2, fire_station_pdf))

The data is still spread with constant widths, but now the widths changed

a2 <- nchar("Hildesheim ")
b2 <- nchar("An der Feuerwache 4-7 ")
c2 <- nchar("31135 Hildesheim ")
d2 <- nchar("05121.3012222 ")

extract <- paste0(extract1, extract2)

fire_stations <- data.frame(city = as.character(),
street = as.character(),
place = as.character(),
telephone = as.character(),
fax = as.character())
row_to_insert <- data.frame(city = "city",
street = "street",
place = "place",
telephone = "telephone",
fax = "fax")

for (i in 1:(length(gregexpr(pattern = "\n ", text = extract)[[1]]) + 1)) { # add one since last row does not have a \n

stopper <- gregexpr(pattern = "\n ", text = extract)[[1]][1]
if (stopper == -1) {
stopper <- nchar(extract)
}
substring <- substr(extract, start = 1, stop = stopper)

fixed spacing changed between two extracts

if (i == length(gregexpr(pattern = "\n ", text = extract1)[[1]]) + 1) {
a <- a2
b <- b2
c <- c2
d <- d2
}

city <- trimws(substr(substring, start = 1, stop = a), which = "both")
street <- trimws(substr(substring, start = a + 1, stop = a + b), which = "both")
place <- trimws(substr(substring, start = a + b + 1, stop = a + b + c), which = "both")
telephone <- trimws(substr(substring, start = a + b + c + 1, stop = a + b + c + d), which = "both")
fax <- trimws(substr(substring, start = a + b + c + d + 1, stop = stopper), which = "both")

row_to_insert <- data.frame(city = city,
street = street,
place = place,
telephone = telephone,
fax = fax)
fire_stations <- rbind(fire_stations, row_to_insert)
extract <- substr(extract, start = stopper + 1, stop = nchar(extract))
}

Get locations

fire_stations$Adr1 <- paste("Germany", fire_stations$city, fire_stations$street, sep = ", ")
fire_stations$Adr2 <- paste("Germany", fire_stations$place, fire_stations$street, sep = ", ")

Cleaning the adressess a bit

fire_stations$Adr1 <- adress.cleaner(text_in = fire_stations$Adr1)
fire_stations$Adr2 <- adress.cleaner(text_in = fire_stations$Adr2)
fire_stations$Adr3 <- gsub("-", " ", fire_stations$Adr1)

Unable to find an inherited method for function ‘fromJSON’

Hi,

I am new to photon and getting this error:

0Error in (function (classes, fdef, mtable) :
unable to find an inherited method for function ‘fromJSON’ for signature ‘"NULL", "missing"

I am getting this error by using the geocode example.

address <- c("19 rue Michel Bakounine, 29600 Morlaix, France",
"5 rue Proudhon, 34130 Mauguio France",
"2 Emma Goldmanweg, Tilburg, Netherlands",
"36 Strada Panait Israti, Bucarest, Romania")
place <- geocode(address, limit = 1, key = "place")
place

Thank you,
Alex

Not working?

I can't get this to work on Rstudio Version 0.99.902
I run the code and the package successfully installs but it fails to create a geocode function.

Limit by country

Hi there,

I'm not sure if this is the right place but I wrote a small geocoder plugin to OpenLayers 3 and would like to know about limiting the search by country. I don't see anything about this feature in main page.

Best regards.

Arguments are not allowed for R version 3.6.1

loc <- geocode(df$address, limit = 1, key = "place")

Error in geocode(df$address, limit = 1, key = "place") :
unused arguments (limit = 1, key = "place")

R_version
platform x86_64-w64-mingw32
arch x86_64
os mingw32
system x86_64, mingw32
status
major 3
minor 6.1
year 2019
month 07
day 05
svn rev 76782
language R
version.string R version 3.6.1 (2019-07-05)
nickname Action of the Toes

Update to the API address

Hi,

thanks for this wonderfully convenient package!
I noticed, that as of soon it might stop working, or already did so, because of a change in the API of photon.
Specifically, the default server needs to be changed to: "https://photon.komoot.io/".

Thanks again and all the best!

#16

Geolocate within a specific place (city, country...)

I would like to geolocate addresses from a particular city.

I have just recently used photon R package as a geolocation solution with certain degree of success. Unfortunately, some results are placed very far away from the place I am trying to geolocate, even if I use location bias:

`df = geocode(address, limit = 1,key = "highway", locbias = c(-0.8787, 41.6486))`

As far as I know Photon can accept keys and values, but don't know how to use it, further than what I did (If I am not wrong it will search only OSM's objects tagged as highway=*). Is there a way to tell phonon to search only OSM's objects tagged as highway=* AND are located within a city?

Error: $ operator is invalid for atomic vectors

Sometimes {photon} does not work properly. I have the following error :
Error: $ operator is invalid for atomic vectors.

df1 <- photon::geocode(df2$address, limit = 1, key = "place")

df2 is tbl_df() or data.frame() and nrow(df2) = 7006.
However, another day this script can work fine with nrow(df2) = 18000.

R version :
platform x86_64-w64-mingw32
arch x86_64
os mingw32
system x86_64, mingw32
status
major 3
minor 6.1
year 2019
month 07
day 05
svn rev 76782
language R
version.string R version 3.6.1 (2019-07-05)
nickname Action of the Toes

Results for NYC are completely off

Hi,

And thank you for the R plugin.

However, I have severe issues with the accuracy of the geocoding.
Here is the detail:

  1. I have installed the last version of Photon server on our server, with the latest data, yesterday
  2. I have several addresses in NYC I want to check and geocode. They are saved as an R dataframe under this form, which is the standardized address form in the US:
1000 Richmond Terrace, New York, NY 10301 United States
75 Ninth Avenue, New York, NY 10011 United States
536 West 112th Street, New York, NY 10025 United States
90 West Houston Street, New York, NY 10012 United States
1008 Manhattan Ave, New York, NY 11222 United States
1700 Second Ave, New York, NY 10128 United States
52 Prince Street, New York, NY 10012 United States
225 Madison Avenue, New York, NY 10016 United States
147 Greene Avenue, New York, NY 11238 United States
228 West 47th Street, New York, NY 10036 United States
579 5th avenue, New York, NY 11215 United States
116 East 59th Street, New York, NY 10022 United States
162 Fifth Avenue, New York, NY 11217 United States
173 7th Ave. South, New York, NY 10014 United States

They are all valid addresses, coming from a local government agency.

  1. The addresses belong to a dataframe, plctst, in the ads column. Then I geocode them using my server, thus:
adsnrm <- photon::geocode(plctst$ads, limit = 1, server = <my-server-url>, lang = "en", key = "building")
  1. The results are completely off.
    I attach the csv file for the 50 first random addresses: geocode_5_tst.csv

While I can understand the results for some of the addresses, which are obviously malformed (e.g. row 3), I cannot understand why simple addresses yield results so wrong...

Of course, I have tried:

  • using different key parameters: place, no key > the same
  • removing the lang parameter altogether > the same
  • removing the postal code within the address > the same
  • changing the order of the components in each address, such as described in #5 > same result

And I am not sure the errors belong to the plugin or to the server.

Please let me know what I am doing wrong.
Thanks,
S.

P.S. Je suis français, vivant aux US. Si vous souhaitez continuer en français, dites-moi.

new issue

Hello, Using this script:

  loc <- c("19 rue Michel Bakounine, 29600 Morlaix, France",
     "5 rue Proudhon, 34000 Montpellier, France",
     "2 Emma Goldmanweg, Tilburg, Netherlands",
     "36 Strada Panait Israti, Bucarest, Romania")
  geocode(loc, limit = 1, key = "place")

give me this error:

   Error in (function (classes, fdef, mtable)  : 
   unable to find an inherited method for function ‘fromJSON’ for signature ‘"NULL", "missing"’

Error in file(con, "r") : cannot open the connection

Hi, the geocode function gives me error:

address <- c("somewhere 9, some country") place <- geocode(address, limit = 1, key = "place", server = "http://nominatim.openstreetmap.org/")

Error in file(con, "r") : cannot open the connection

Any idea?

OK seems, I confused the idea of nominatim and photon.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.