Extract address components from coordiantes

后端 未结 3 585
有刺的猬
有刺的猬 2021-01-27 05:15

I\'m trying to reverse geocode with R. I first used ggmap but couldn\'t get it to work with my API key. Now I\'m trying it with googleway.

newframe[,c("Front         


        
3条回答
  •  梦毁少年i
    2021-01-27 05:37

    After reverse geocoding into newframe$address the address components could be extracted further as follows:

    # Make a boolean array of the valid ("OK" status) responses (other statuses may be "NO_RESULTS", "REQUEST_DENIED" etc).
    sel <- sapply(c(1: nrow(newframe)), function(x){
      newframe$address[[x]]$status == 'OK'
    })
    
    # Get the address_components of the first result (i.e. best match) returned per geocoded coordinate.
    address.components <- sapply(c(1: nrow(newframe[sel,])), function(x){
      newframe$address[[x]]$results[1,]$address_components
    })
    
    # Get all possible component types.
    all.types <- unique(unlist(sapply(c(1: length(address.components)), function(x){
      unlist(lapply(address.components[[x]]$types, function(l) l[[1]]))
    })))
    
    # Get "long_name" values of the address_components for each type present (the other option is "short_name").
    all.values <- lapply(c(1: length(address.components)), function(x){
      types <- unlist(lapply(address.components[[x]]$types, function(l) l[[1]]))
      matches <- match(all.types, types)
      values <- address.components[[x]]$long_name[matches]
    })
    
    # Bind results into a dataframe.
    all.values <- do.call("rbind", all.values)
    all.values <- as.data.frame(all.values)
    names(all.values) <- all.types
    
    # Add columns and update original data frame.
    newframe[, all.types] <- NA
    newframe[sel,][, all.types] <- all.values
    

    Note that I've only kept the first type given per component, effectively skipping the "political" type as it appears in multiple components and is likely superfluous e.g. "administrative_area_level_1, political".

提交回复
热议问题