2012-01-05 17 views
15

में राज्य संहिता को समन्वयित करता है क्या आर में राज्य कोडों में अक्षांश और देशांतर निर्देशांक को परिवर्तित करने का कोई तेज़ तरीका है? मैं ज़िप तालिका को एक लुकअप टेबल के रूप में उपयोग कर रहा हूं लेकिन जब मैं बहुत सारे अक्षांश/लंबे मूल्यों की पूछताछ कर रहा हूं तो यह बहुत धीमा हैअक्षांश रेखांश आर

यदि आर में नहीं है तो Google geocoder या किसी अन्य प्रकार का उपयोग करके ऐसा करने का कोई तरीका है तेजी से पूछताछ सेवा?

धन्यवाद!

+0

भी मेरा उत्तर यहाँ देख में कर सकते हैं, ggmap :: revgeocode': https://stackoverflow.com/questions/46150851/how-to-get-california- काउंटी स्थान-से-अक्षां इट्यूड-एंड-रेखांश-सूचना/46151310 # 46151310 –

उत्तर

32

यहां एक ऐसा फ़ंक्शन है जो निचले 48 राज्यों में लेट-लांग का डेटा लेता है, और प्रत्येक बिंदु के लिए, वह राज्य लौटाता है जिसमें यह स्थित है।

library(sp) 
library(maps) 
library(maptools) 

# The single argument to this function, pointsDF, is a data.frame in which: 
# - column 1 contains the longitude in degrees (negative in the US) 
# - column 2 contains the latitude in degrees 

latlong2state <- function(pointsDF) { 
    # Prepare SpatialPolygons object with one SpatialPolygon 
    # per state (plus DC, minus HI & AK) 
    states <- map('state', fill=TRUE, col="transparent", plot=FALSE) 
    IDs <- sapply(strsplit(states$names, ":"), function(x) x[1]) 
    states_sp <- map2SpatialPolygons(states, IDs=IDs, 
        proj4string=CRS("+proj=longlat +datum=WGS84")) 

    # Convert pointsDF to a SpatialPoints object 
    pointsSP <- SpatialPoints(pointsDF, 
        proj4string=CRS("+proj=longlat +datum=WGS84")) 

    # Use 'over' to get _indices_ of the Polygons object containing each point 
    indices <- over(pointsSP, states_sp) 

    # Return the state names of the Polygons object containing each point 
    stateNames <- sapply([email protected], function(x) [email protected]) 
    stateNames[indices] 
} 

# Test the function using points in Wisconsin and Oregon. 
testPoints <- data.frame(x = c(-90, -120), y = c(44, 44)) 

latlong2state(testPoints) 
[1] "wisconsin" "oregon" # IT WORKS 
+2

इस उदाहरण को काम करने के लिए मुझे wgs84 को WGS84 में बदलना पड़ा। – lever

+0

@lever इसे इंगित करने के लिए धन्यवाद। आश्चर्य जब (और कहाँ) बदल दिया गया था। किसी भी मामले में, अब मैंने इसे ठीक करने के लिए संपादित किया है। –

2

एसपी पैकेज में देखें? आपको स्पेसियल पॉलीगॉनडेटा फ्रेम के रूप में राज्य सीमाएं रखने की आवश्यकता होगी।

4

आप:

समारोह के अधिकांश बस SpatialPoints और SpatialPolygons वस्तुओं sp पैकेज में over() समारोह है, जो अंक और बहुभुज की 'चौराहे' की गणना के वास्तविक भार उठाने करता है की जरूरत को तैयार करता है का उपयोग करते हुए `आर

की कुछ लाइनें
library(sp) 
library(rgdal) 
#lat and long 
Lat <- 57.25 
Lon <- -9.41 
#make a data frame 
coords <- as.data.frame(cbind(Lon,Lat)) 
#and into Spatial 
points <- SpatialPoints(coords) 
#SpatialPolygonDataFrame - I'm using a shapefile of UK counties 
counties <- readOGR(".", "uk_counties") 
#assume same proj as shapefile! 
proj4string(points) <- proj4string(counties) 
#get county polygon point is in 
result <- as.character(over(points, counties)$County_Name) 
+0

धन्यवाद! यह भी आसान था :) –