繁体   English   中英

计算 shapefile 中的密度

[英]Calculating density within a shapefile

我正在尝试计算 shapefile 中的密度,但我很有信心我做错了。 这个想法是通过密度找出哪些地理区域的销售额最高。

这是我使用的文件的链接(testdata.shp)

library(sf)

sample <- st_read("testdata.shp")

sample$area <- st_area(sample$geometry)

density_calc <-sample %>% st_buffer(0) %>% group_by(areas) %>% summarise(`Sales (density)` = sum(sales)/sum(area))

以下是 shapefile 的详细信息:

Geometry set for 2106 features 
geometry type:  MULTIPOLYGON
dimension:      XY
bbox:           xmin: -120.0065 ymin: 35.00184 xmax: -114.0396 ymax: 42.00221
epsg (SRID):    4326
proj4string:    +proj=longlat +datum=WGS84 +no_defs

我想我的问题是,我真的不知道什么是对什么是错,所以我不知道我是否做对了。

对不起,如果这不是最广泛的问题,我只是不太记得我的高中几何!

raster包有助于使这个计算变得非常简单,就像在R使用 data.frame 一样:

library(raster)
list.files(workDir)
test_shp <- shapefile(file.path(workDir, 'testdata.shp'))
names(test_shp)
#[1] "distrct"       "sbdstrc"       "terrtry"      
#[4] "region"        "turf"          "sales"        
#[7] "leads"         "cnvrsns"       "areas" 

sum(is.na(test_shp$sales)) #note that 346 polygons have no sales data

#get the area as square kilometers
test_shp$km2 <- area(test_shp) / 10000

#calc the sales density
test_shp$sales_density <- test_shp$sales / test_shp$km2

#calculate the 25th, 50th, and 75th percentile of all polygons
quartiles <- quantile(test_shp$sales_density, probs=c(0.25, 0.5, 0.75), na.rm=TRUE) 

#plot the result, coloring by which percentile the sales density is for a given polygon 
plot(test_shp, col=ifelse(is.na(test_shp$sales_density), 'gray', ifelse(test_shp$sales_density >= quartiles[3], 'dark green', ifelse(test_shp$sales_density >= quartiles[2], 'light green', ifelse(test_shp$sales_density >= quartiles[1], 'yellow', 'red')))), border='transparent')  (eg. >75th, 50-75th, etc.)

#add the legend
legend('bottomleft', legend=c('Q4', 'Q3', 'Q2', 'Q1', 'No data'), pch=15, col=c('dark green', 'light green', 'yellow', 'red', 'gray'))

销售密度结果

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM