• Welcome to LiuJason's Blog!

在R中使用API将地名转换为经纬度坐标

R语言 Jason 5 years ago (2019-11-04) 1199 Views 0 Comments QR code of this page

最近在分析全球癌症发病率与特定地理位置直接的关系,用到了CI5数据集。但是这个数据集中只有癌症登记机构的地名信息,而进行分析的话需要用到坐标,因此需要将地名转换为经纬度坐标。

这里我找到了一个非常棒的API,只要注册账号即可免费转换,每天限制1500条:

注册地址:【已隐藏】

注册后在账户中可以看到API Key,在每次curl请求的时候附上这个key即可。下附代码:

#convert city names to coordinates
library(jsonlite)
region_list <- registry_info[grep(":", registry_info$discription, invert =T), ][,1:2]
region_list$latitude <- NA
region_list$longitude <- NA
for(i in 1:length(region_list$discription)){
#for(i in 1:5){
  tryCatch({
    region_info_raw <- fromJSON(gsub(" ", "%20", 
                                     paste0("https://api.【已隐藏】.com/geocode/v1/json?q=",
                                            region_list$discription[i],
                                            "&key=【已隐藏】"), fixed = T))
    region_list$latitude[i] <- region_info_raw$results$geometry[1,"lat"]
    region_list$longitude[i] <- region_info_raw$results$geometry[1,"lng"]
  }, error=function(e){cat("ERROR for", region_list$discription[i], i, ":", conditionMessage(e), "\n")})
}
region_list$latitude[152] <- 37.1984436
region_list$longitude[152] <- 55.070672

write.csv(region_list, 
          file = "./datasets/CI5-XId/converted/registry_info.csv", quote = T, 
          row.names = F)

另附说明文档:

Request Parameters

Required Parameters

key a 30 character long, alphanumeric string.Your API key is 【已隐藏】You can generate a new key in your account dashboard.Free trial accounts are limited to one key per account, paid accounts can have multiple keys.
q the query string to be geocodeda latitude, longitude or a placename/address

The query should be URL encoded, so spaces should be a +, comma should be %2C, instead of, for example, non-ascii strings like München you should send us M%C3%BCnchen.

When reverse geocoding the query should be in latitude, longitude order in decimal format. For example: 51.952659, 7.632473. There is no reason to send more than six or seven digits past the decimal as that then gets down to the precision of a centimeter. See Wikipedia about decimal degree precision. If impossible coordinates are supplied you will receive a 400 invalid coordinates as the response code in the status section of the response.

When forward geocoding there are many things you can do to help improve our chance of finding the correct answer and answering more quickly. Please see the optional parameters below, and the best practices - especially our detailed guide on how to format your forward geocoding query.

Optional Parameters

abbrv When set to 1 we attempt to abbreviate and shorten the formatted string we return. Learn more about formatted placenames.
add_request When set to 1 the various request parameters are added to the response for ease of debugging.
bounds Used only for forward geocoding. This value will restrict the possible results to a defined bounding box.The value of the bounds parameter should be specified as two coordinate points forming the south-west and north-east corners of a bounding box. For example: bounds=-0.563160,51.280430,0.278970,51.683979 (min lon, min lat, max lon, max lat).Values that are not valid coordinates are ignored.We have built a small, map-based tool to easily see bounds values. We hope it helps.
countrycode Used only for forward geocoding. Restricts results to the specified country/territory or countries.The country code is a two letter code as defined by the ISO 3166-1 Alpha 2 standard. E.g. gb for the United Kingdom, fr for France, us for United States.Non-two letter country codes are ignored.You can specify multiple country codes by supplying a comma separated list. For example countrycode=ca,us would limit results to either the United States or Canada.Please note, many territories have their own ISO 3116-1 codes, despite being part of another country. An example is Puerto Rico which has ISO code PR, despite being part of the United States, US. In the components portion of results we return both - see details below.Many parts of the world have complex or even disputed political structures and/or share postal systems with another country, and thus may be treated as a single or multiple country by some of the geocoders we rely upon. It may make sense to specify multiple country codes.As an example, when searching for locations on the island of Aruba - technically a constituent country of the Kingdom of the Netherlands - we will do better if you specify countrycode=aw,nl rather than just countrycode=aw.As a convenience we have compiled a list of country codes for dependent territories.
jsonp Wraps the returned JSON with a function name.
language An IETF format language code (such as es for Spanish or pt-BR for Brazilian Portuguese), or native in which case we will attempt to return the response in the local language(s).If no language is explicitly specified, we will then look for an HTTP Accept-Language header like those sent by a browser and use highest quality language specified (please see RFC 4647 for details). If the request did not specify a valid header, then en (English) will be assumed.Please note, setting the language parameter does NOT mean results will only be returned in the specified language. Instead it means we will attempt to favour results in that language.Please see our detailed comments on language below, particularly the caviats around specifying language=native.
limit The maximum number of results we should return. Default is 10. Maximum allowable value is 100.
min_confidence An integer from 1-10. Only results with at least this confidence will be returned. Learn more about our confidence score.
no_annotations When set to 1 results will not contain annotations.The only exception is if the optional roadinfo parameter is set (see below).
no_dedupe When set to 1 results will not be deduplicated.
no_record When set to 1 the query contents are not logged. Please use if you have concerns about privacy and want us to have no record of your query.
pretty When set to 1 results are 'pretty' printed for easier reading. Useful for debugging.
proximity Used only for forward geocoding. Provides the geocoder with a hint to bias results in favour of those closer to the specified location. Please note though, this is just one of many factors in the internal scoring we use for ranking results. The value is a point with latitude, longitude coordinates in decimal format. For example: 51.952659, 7.632473Values that are not valid coordinates are ignored.
roadinfo When set to 1 the behaviour of the geocoder is changed to attempt to match the nearest road (as opposed to address). If possible we also fill additional information in the roadinfo annotation. Please see details below.

Response Format

Reverse Geocoding Response Format

The response from the reverse geocoder is formatted according to the contents of the format component of the request URL.

Note: all returned coordinates use WGS 84 (sometimes also known as EPSG:4326) as reference coordinate system.

JSON Output

In the following example, a response in JSON format is requested to get the nearest address for coordinates -22.6792, 14.5272.

https://api.【已隐藏】.com/geocode/v1/json?q=-22.6792%2C+14.5272&key=【已隐藏】&pretty=1

The JSON returned by the reverse geocoder will look like:

{
   "documentation" : "https://【已隐藏】.com/api",
   "licenses" : [
      {
         "name" : "see attribution guide",
         "url" : "https://【已隐藏】.com/credits"
      }
   ],
   "rate" : {
      "limit" : 15000,
      "remaining" : 14694,
      "reset" : 1563235200
   },
   "results" : [
      {
         "annotations" : {
            "DMS" : {
               "lat" : "22\u00b0 40' 45.05736'' S",
               "lng" : "14\u00b0 31' 36.48576'' E"
            },
            "MGRS" : "33KVQ5139191916",
            "Maidenhead" : "JG77gh36fx",
            "Mercator" : {
               "x" : 1617116.157,
               "y" : -2576798.589
            },
            "OSM" : {
               "edit_url" : "https://www.【已隐藏】.org/edit?node=4488973891#map=17/-22.67918/14.52680",
               "url" : "https://www.【已隐藏】.org/?mlat=-22.67918&mlon=14.52680#map=17/-22.67918/14.52680"
            },
            "UN_M49" : {
               "regions" : {
                  "AFRICA" : "002",
                  "NA" : "516",
                  "SOUTHERN_AFRICA" : "018",
                  "SUB-SAHARAN_AFRICA" : "202",
                  "WORLD" : "001"
               },
               "statistical_groupings" : [
                  "LEDC"
               ]
            },
            "callingcode" : 264,
            "currency" : {
               "alternate_symbols" : [
                  "N$"
               ],
               "decimal_mark" : ".",
               "disambiguate_symbol" : "N$",
               "html_entity" : "$",
               "iso_code" : "NAD",
               "iso_numeric" : "516",
               "name" : "Namibian Dollar",
               "smallest_denomination" : 5,
               "subunit" : "Cent",
               "subunit_to_unit" : 100,
               "symbol" : "$",
               "symbol_first" : 0,
               "thousands_separator" : ","
            },
            "flag" : "\ud83c\uddf3\ud83c\udde6",
            "geohash" : "k7fqfx6h5jbq5tn8tnpn",
            "qibla" : 31.02,
            "roadinfo" : {
               "drive_on" : "left",
               "road" : "Woermann St",
               "speed_in" : "km/h"
            },
            "sun" : {
               "rise" : {
                  "apparent" : 1563169320,
                  "astronomical" : 1563164580,
                  "civil" : 1563167880,
                  "nautical" : 1563166200
               },
               "set" : {
                  "apparent" : 1563208440,
                  "astronomical" : 1563213180,
                  "civil" : 1563209880,
                  "nautical" : 1563211560
               }
            },
            "timezone" : {
               "name" : "Africa/Windhoek",
               "now_in_dst" : 0,
               "offset_sec" : 7200,
               "offset_string" : "+0200",
               "short_name" : "CAT"
            },
            "what3words" : {
               "words" : "integrate.laughter.teller"
            }
         },
         "bounds" : {
            "northeast" : {
               "lat" : -22.6790826,
               "lng" : 14.5269016
            },
            "southwest" : {
               "lat" : -22.6792826,
               "lng" : 14.5267016
            }
         },
         "components" : {
            "ISO_3166-1_alpha-2" : "NA",
            "ISO_3166-1_alpha-3" : "NAM",
            "_type" : "restaurant",
            "city" : "Swakopmund",
            "continent" : "Africa",
            "country" : "Namibia",
            "country_code" : "na",
            "restaurant" : "Beryl's Restaurant",
            "road" : "Woermann St",
            "state" : "Erongo Region",
            "suburb" : "Central"
         },
         "confidence" : 9,
         "formatted" : "Beryl's Restaurant, Woermann St, Swakopmund, Namibia",
         "geometry" : {
            "lat" : -22.6791826,
            "lng" : 14.5268016
         }
      }
   ],
   "status" : {
      "code" : 200,
      "message" : "OK"
   },
   "stay_informed" : {
      "blog" : "https://blog.【已隐藏】.com",
      "twitter" : "https://twitter.com/【已隐藏】"
   },
   "thanks" : "For using an OpenCage API",
   "timestamp" : {
      "created_http" : "Mon, 15 Jul 2019 14:58:08 GMT",
      "created_unix" : 1563202688
   },
   "total_results" : 1
}

Forward Geocoding Response Format

The response from the geocoder is returned according to the contents of the format component of the request URL.

JSON Output

In the following example, a response in JSON format is requested to get the coordinates of Carapicuíba, Brasil.

https://api.opencagedata.com/geocode/v1/json?q=Rua+Cafel%C3%A2ndia%2C+Carapicu%C3%ADba%2C+Brasil&key=【已隐藏】&pretty=1

The JSON returned by the geocoder will look like:

{
   "documentation" : "https://【已隐藏】.com/api",
   "licenses" : [
      {
         "name" : "see attribution guide",
         "url" : "https://【已隐藏】.com/credits"
      }
   ],
   "rate" : {
      "limit" : 15000,
      "remaining" : 14698,
      "reset" : 1563235200
   },
   "results" : [
      {
         "annotations" : {
            "DMS" : {
               "lat" : "23\u00b0 32' 13.34076'' S",
               "lng" : "46\u00b0 50' 8.35980'' W"
            },
            "MGRS" : "23KLP1260995829",
            "Maidenhead" : "GG66nl91rc",
            "Mercator" : {
               "x" : -5213721.321,
               "y" : -2680037.892
            },
            "OSM" : {
               "edit_url" : "https://www.【已隐藏】.org/edit?way=185327107#map=16/-23.53704/-46.83566",
               "url" : "https://www.【已隐藏】.org/?mlat=-23.53704&mlon=-46.83566#map=16/-23.53704/-46.83566"
            },
            "UN_M49" : {
               "regions" : {
                  "AMERICAS" : "019",
                  "BR" : "076",
                  "LATIN_AMERICA" : "419",
                  "SOUTH_AMERICA" : "005",
                  "WORLD" : "001"
               },
               "statistical_groupings" : [
                  "LEDC"
               ]
            },
            "callingcode" : 55,
            "currency" : {
               "decimal_mark" : ",",
               "html_entity" : "R$",
               "iso_code" : "BRL",
               "iso_numeric" : "986",
               "name" : "Brazilian Real",
               "smallest_denomination" : 5,
               "subunit" : "Centavo",
               "subunit_to_unit" : 100,
               "symbol" : "R$",
               "symbol_first" : 1,
               "thousands_separator" : "."
            },
            "flag" : "\ud83c\udde7\ud83c\uddf7",
            "geohash" : "6gydn5pqf1uhc9v6p44f",
            "qibla" : 69.01,
            "roadinfo" : {
               "drive_on" : "right",
               "road" : "Rua Cafel\u00e2ndia",
               "road_type" : "residential",
               "speed_in" : "km/h"
            },
            "sun" : {
               "rise" : {
                  "apparent" : 1563184080,
                  "astronomical" : 1563179340,
                  "civil" : 1563182640,
                  "nautical" : 1563180960
               },
               "set" : {
                  "apparent" : 1563223080,
                  "astronomical" : 1563227820,
                  "civil" : 1563224520,
                  "nautical" : 1563226200
               }
            },
            "timezone" : {
               "name" : "America/Sao_Paulo",
               "now_in_dst" : 0,
               "offset_sec" : -10800,
               "offset_string" : "-0300",
               "short_name" : "BRT"
            },
            "what3words" : {
               "words" : "gosh.copiers.stops"
            }
         },
         "bounds" : {
            "northeast" : {
               "lat" : -23.5370391,
               "lng" : -46.8356555
            },
            "southwest" : {
               "lat" : -23.5373733,
               "lng" : -46.8374629
            }
         },
         "components" : {
            "ISO_3166-1_alpha-2" : "BR",
            "ISO_3166-1_alpha-3" : "BRA",
            "_type" : "road",
            "city" : "Carapicu\u00edba",
            "city_district" : "Carapicu\u00edba",
            "continent" : "South America",
            "country" : "Brazil",
            "country_code" : "br",
            "county" : "Regi\u00e3o Imediata de S\u00e3o Paulo",
            "postcode" : "06321-665",
            "road" : "Rua Cafel\u00e2ndia",
            "road_type" : "residential",
            "state" : "S\u00e3o Paulo",
            "state_code" : "SP",
            "state_district" : "Regi\u00e3o Intermedi\u00e1ria de S\u00e3o Paulo",
            "suburb" : "Parque Jos\u00e9 Alexandre"
         },
         "confidence" : 9,
         "formatted" : "Rua Cafel\u00e2ndia, Carapicu\u00edba - SP, 06321-665, Brazil",
         "geometry" : {
            "lat" : -23.5370391,
            "lng" : -46.8356555
         }
      }
   ],
   "status" : {
      "code" : 200,
      "message" : "OK"
   },
   "stay_informed" : {
      "blog" : "https://blog.【已隐藏】.com",
      "twitter" : "https://twitter.com/【已隐藏】"
   },
   "thanks" : "For using an OpenCage API",
   "timestamp" : {
      "created_http" : "Mon, 15 Jul 2019 14:58:06 GMT",
      "created_unix" : 1563202686
   },
   "total_results" : 1
}

This article is under CC BY-NC-SA 4.0 license.
Please quote the original link:https://www.liujason.com/article/339.html
Like (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址