簡體   English   中英

找出一組多邊形R中每個多邊形的最大點

[英]Find maximum point with in each polygon for a set of polygons R

我確定這個問題已經在其他地方得到了解答,但是我無法通過搜索提出。

我有代表一個國家內的城市以及每個城市人口的點。 我也有一個縣的多邊形文件。 我想找到每個縣內最大城市的位置。

如何才能做到這一點?

這是一些數據

結構(列表(國家= c(“ us”,“ us”,“ us”,“ us”,“ us”,“ us”,“ us”,“ us”,“ us”,“ us”,“ us ”,
“我們”,“我們”,“我們”,“我們”,“我們”,“我們”,“我們”,“我們”,“我們”,“我們”,“我們”,“我們”,“我們“,”我們“),城市= c(” cabarrus“,” cox store“,” cal-vel“,” briarwood townhouses“,” barker heights“,” davie
十字路口”,“蟹點村”,“ azalea”,“ chesterfield”,“ charlesmont”,“ connor”,“ clover garden”,“ corriher heights”,“ callisons”,“ crestview acres”,“ clegg”,“ canaan”公園”,“尚蒂伊”,“貝爾格萊德”,“ brices十字路口”,“虛張聲勢”,“ butner”,“ bottom”,“ bandy”,“ bostian heights”),AccentCity = c(“ Cabarrus”,“ Cox Store” ,“ Cal-Vel”,“ Briarwood Townhouses”,“ Barker Heights”,“ Davie Crossroads”,“ Crab Point Village”,“ Azalea”,“ Chesterfield”,“ Charlesmont”,“ Connor”,“ Clover Garden”,“ Corriher Heights”,“ Callisons”,“ Crestview Acres”,“ Clegg”,“ Canaan Park”,“ Chantilly”,“ Belgrade”,“ Brices Crossroads”,“ Bluff”,“ Butner”,“ Bottom”,“ Bandy” ,“ Bostian Heights”),區域= c(“ NC”,“ NC”,“ NC”,“ NC”,“ NC”,“ NC”,“ NC”,“ NC”,“ NC”,“ NC” ,“ NC”,“ NC”,“ NC”,“ NC”,“ NC”,“ NC”,“ NC”,“ NC”,“ NC”,“ NC”,“ NC”,“ NC”,“ NC“,” NC“,” NC“),人口= c(NA_integer_,NA_integer_,NA_integer_,NA_integer_,NA_integer_,NA_integer_,NA_integer_,NA_integer_,NA_integer_,NA_integer_,NA_integer_,NA_integer_, _integer_,NA_integer_,NA_integer_,NA_integer_,NA_integer_,NA_integer_,NA_integer_,NA_integer_,NA_integer_,NA_integer_,NA_integer_,NA_integer_,NA_integer_),緯度=(35.2369444,35.275,36.4291667,35.295,35.3111111,35.8319444,34.7602778,35.58,35.81,5.9341667, 35.7419444、36.1883333、35.5605556、35.0841667、35.0213889、35.8655556、36.2761111、36.3016667、34.88、34.8186111、35.8377778、36.1319444、36.4747222、35.6419444、35.7544444),經度= c(-80.5419444,-82.0352778,-72.5238667444) -80.535,-76.7305556,-82.4713889,-81.6611111,-81.5127778,-78.1486111,-79.4630556,-80.635,-76.7255556,-80.5427778,-78.8497222,-79.7852778,-76.1711111,-77.2352778,-78.1016667,-82.8580556556,-7 ,-80.7741667,-81.09,-80.9294444))。.names = c(“國家/地區”,“城市”,“ AccentCity”,“區域”,“人口”,“緯度”,“經度”),row.names = c(544L,889L,551L,434L,190L,975L,894L,147L,717L,700L,831L,773L,862L,559L,915L,753L,584L,695L, 262L,437L,372L,537L,406L,178L,02L),類=“ data.frame”)

以及在北卡羅來納州閱讀的一些代碼

xx <- readShapePoly(system.file("shapes/sids.shp", package="maptools")[1],
                IDvar="FIPSNO", proj4string=CRS("+proj=longlat +ellps=clrk66"))

plot(xx)

我想找到每個縣內人口最多的城市。 對不起,我沒有可復制的示例。 如果我做到了,我將得到答案!

簡短的答案是,您應該在包rgeos使用gContains(...)

這是一個很長的答案。

在下面的代碼中,我們從GADM數據庫中獲取北卡羅來納州縣的高分辨率shapefile,並從美國地質調查局數據庫中獲取北卡羅來納州城市的地理編碼數據集。 后者已經有了縣的信息,但是我們忽略了這一點。 然后,我們使用gContains(...)將城市映射到相應的縣,將信息添加到城市數據框中,並使用data.table包確定每個縣中最大的城市。 大部分工作都在末尾的4行代碼中。

library(raster)   # for getData(...);   you may not need this
library(foreign)  # for read.dbf(...);  you may not need this
library(rgeos)    # for gContains(...); loads package sp as well

setwd("< directory for downloaded data >")
# get North Carolina Counties shapefile from GADM database
USA         <- getData("GADM",country="USA",level=2)   # level 2 is counties
NC.counties <- USA[USA$NAME_1=="North Carolina",]      # North Carolina Counties
# get North Carolina Cities data from USGS database
url <- "http://dds.cr.usgs.gov/pub/data/nationalatlas/citiesx010g_shp_nt00962.tar.gz"
download.file(url,"cities.tar.gz")
untar("cities.tar.gz")
data      <- read.dbf("citiesx010g.dbf",as.is=TRUE)
NC.data   <- data[data$STATE=="NC",c("NAME","COUNTY","LATITUDE","LONGITUDE","POP_2010")]
## --- evverything up to here is just to set up the example

# convert cities data.frame to SpatialPointsDataFrame
NC.cities <- SpatialPointsDataFrame(NC.data[,c("LONGITUDE","LATITUDE")],
                                    data=NC.data,
                                    proj4string=CRS(proj4string(NC.counties)))
# map cities to counties
city.cnty   <- gContains(NC.counties,NC.cities,byid=TRUE)
# add county information to cities data
NC.data$county <- apply(city.cnty,1,function(cnty)ifelse(any(cnty),NC.counties@data[cnty,]$NAME_2,NA))
# identify largest city in each county
library(data.table)
result <- setDT(NC.data)[,.SD[which.max(POP_2010)],by="county"]
head(result)
#      county             NAME   COUNTY LATITUDE LONGITUDE POP_2010
# 1:  Jackson        Cullowhee  Jackson 35.31371 -83.17653     6228
# 2:   Graham     Robbinsville   Graham 35.32287 -83.80740      620
# 3:   Wilkes North Wilkesboro   Wilkes 36.15847 -81.14758     4245
# 4:    Rowan        Salisbury    Rowan 35.67097 -80.47423    33662
# 5: Buncombe        Asheville Buncombe 35.60095 -82.55402    83393
# 6:    Wayne        Goldsboro    Wayne 35.38488 -77.99277    36437

這條線是這里的主力:

city.cnty   <- gContains(NC.counties,NC.cities,byid=TRUE)

它將SpatialPointsDataFrame NC.Cities中的每個點與SpatialPolygonsDataFrame NC.Cities中的每個多邊形進行NC.counties並返回一個邏輯矩陣,其中行代表城市,列代表縣,如果城市i位於[i,j]元素為TRUE 。縣j ,否則為FALSE 我們在下一條語句中逐行處理矩陣:

NC.data$county <- apply(city.cnty,1,function(cnty)ifelse(any(cnty),NC.counties@data[cnty,]$NAME_2,NA))

使用連續的每一NC.counties的屬性表建立NC.counties以提取縣名稱。

您在問題中提供的數據存在一些問題,這些問題仍然具有啟發性。 首先, maptools軟件包中的NC shapefile分辨率較低。 特別是,這意味着某些沿海島嶼完全消失了,因此這些島嶼之一上的任何城市都不會映射到一個縣。 您的真實數據可能會遇到同樣的問題,因此請當心。

其次,將原始USGS數據集中的COUNTY列與我們添加的county列進行比較,有3個(共865個)縣意見不一致。 事實證明,在這些情況下,USGS數據庫是錯誤的(或已過期)。 您可能有同樣的問題,所以也要當心。

第三,另外三個城市沒有映射到任何縣。 這些都是沿海城市,可能反映了北卡羅萊納州shapefile中的小錯誤。 你晚上也有這個問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM