纬度经度坐标为R中的州代码

时间:2012-01-05 23:36:14

标签: r latitude-longitude google-geocoder

是否有快速方法将纬度和经度坐标转换为R中的状态代码?我一直在使用zipcode包作为查找表,但是当我查询大量的lat / long值时它太慢了

如果不是在R中有没有办法使用谷歌地理编码器或任何其他类型的快速查询服务?

谢谢!

5 个答案:

答案 0 :(得分:37)

这是一个函数,它在较低的48个状态中获取lat-longs的data.frame,并且对于每个点,返回它所在的状态。

大部分功能只是准备SpatialPoints包中SpatialPolygons函数所需的over()sp个对象,这样可以真正重大计算'{1}}点和多边形的交点:

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(states_sp@polygons, function(x) x@ID)
    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

答案 1 :(得分:9)

你可以在几行R中完成。

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)

答案 2 :(得分:2)

参见sp包中的?over。您需要将状态边界设置为SpatialPolygonDataFrame。

答案 3 :(得分:0)

示例数据(多边形和点)

while (count < 10){
System.out.println("Enter a number between 1 and 100");
            Guess = scan.nextInt();
count++;
     if ( Guess > num1)
    {
        System.out.println("Lower!");
    }
    else if (Guess < num1)
    {
        System.out.println("Higher");
    }
    else {
        break;
    }
}
if(count < 10) {
    System.out.println("Congrats! You have completed it in " + count + " tries!");
} else {
    System.out.println("Sorry you have run out of your 10 guesses!");
}

使用raster :: extract

library(raster)
pols <- shapefile(system.file("external/lux.shp", package="raster"))
xy <- coordinates(p)

答案 4 :(得分:0)

使用sf非常简单:

library(maps)
library(sf)

## Get the states map, turn into sf object
US <- st_as_sf(map("state", plot = FALSE, fill = TRUE))

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

# Make it a spatial dataframe, using the same coordinate system as the US spatial dataframe
testPoints <- st_as_sf(testPoints, coords = c("x", "y"), crs = st_crs(US))

#.. and perform a spatial join!
st_join(testPoints, US)


         ID        geometry
1 wisconsin  POINT (-90 44)
2    oregon POINT (-120 44)