[英]How do I mask a multi-layer netCDF or raster with a single-layer shapefile?
I am currently working with daily precipitation data in.netCDF format.我目前正在处理 .netCDF 格式的每日降水数据。 The data's at a 4km resolution that covers the United States.
数据分辨率为 4 公里,覆盖美国。 However, I want to mask/clip the data with a much higher-resolution shapefile for a particular geographical region (about the size of a county).
但是,我想为特定地理区域(大约一个县的大小)使用更高分辨率的 shapefile 来屏蔽/剪辑数据。 Ultimately, I want the output to be daily precipitation data, either at that high resolution or the original 4km resolution, for the much smaller area.
最终,我希望 output 成为更小区域的每日降水数据,无论是高分辨率还是原始 4km 分辨率。
I've tried a couple different methods, with the most success using the following code:我尝试了几种不同的方法,使用以下代码最成功:
prcp_2000 <- raster::brick('pr_2000.nc')
shapefile <- shapefile("polygon_combined.shp")
shapefile <- spTransform(shapefile, crs(prcp_2000))
prcp_2000 <- mask(prcp_2000, shapefile)
prcp_2000 <- crop(prcp_2000, shapefile)
outfile <- paste("prcp_","2000_","CS",".nc",sep="")
writeRaster(prcp_2000, outfile, overwrite=TRUE, format="CDF", varname="prcp", varunit="mm/day", longname="mm of precipitation per day", xname="lon", yname="lat", zname="day", zunit="days since 1900-01-01")
However, I keep getting nothing but infinities/negative infinities for prcp output, even though I'm still getting appropriate variable lengths otherwise (day=366, lat=24, lon=23).但是,对于 prcp output,我只得到无穷大/负无穷大,即使我仍然得到适当的可变长度(日 = 366,纬度 = 24,经度 = 23)。 Am I missing something?
我错过了什么吗?
The problem is the starting shapefile, shapefile <- shapefile("polygon_combined.shp")
.问题是起始 shapefile,
shapefile <- shapefile("polygon_combined.shp")
。 I imported "polygon_combined.shp" into QGIS to plot a base layer under it easily.我将“polygon_combined.shp”导入 QGIS 到 plot 很容易成为它下面的基础层。 The image I've attached shows that it starts off in the Gulf of Mexico.
我附上的图片显示它从墨西哥湾开始。 With a bad location to begin with, transforming it isn't going to save it later.
开始的位置不好,改造它不会在以后保存它。
I don't know the origin of the data, so I have no idea how it was produced this way to start.我不知道数据的来源,所以我不知道它是如何以这种方式产生的。 One way to possibly fix this is get the data from a different source.
可能解决此问题的一种方法是从不同的来源获取数据。 The EPA makes an ecoregions product that has several levels of regions.
EPA 制作了一个具有多个区域级别的生态区域产品。 I think you want the level IV data to get your region.
我想你想要 IV 级数据来获取你的区域。 You may still need to transform it, but it'll be located in the right place to begin with.
您可能仍需要对其进行转换,但它将位于正确的开始位置。 https://www.epa.gov/eco-research/ecoregion-download-files-state-region-5#pane-47
https://www.epa.gov/eco-research/ecoregion-download-files-state-region-5#pane-47
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.