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)