将CSV文件转换为shapefile-但不希望多边形指向 [英] Converting CSV file to shapefile - but want polygon not points
问题描述
我有一个csv数据框,其坐标为正方形.我已将csv转换为shapefile,但它以四个单独的点进行绘制,并且我希望该图显示一个正方形.
I have a csv dataframe with the coordinates of a square. I have converted the csv into a shapefile, but it plots as four separate points and I want the plot to show a square.
Shape long lat
1 1.1 43 10
2 1.1 43 13
3 1.1 40 13
4 1.1 40 10
我已将其转换为shapefile并为其指定了坐标参考系统,如下所示:
I have converted it into a shapefile and given it a coordinate reference system as follows:
test <- as.data.frame(read_csv("/Users/alan/Desktop/Test.csv"))
Coord_Ref <- st_crs(3035)
plot_locations_test <- st_as_sf(test, coords = c("long", "lat"), crs =Coord_Ref)
然后我可以使用ggplot绘制shapefile
Then I can plot the shapefile using ggplot
ggplot() +geom_sf(data = plot_locations_test)
我得到这个情节
如何使它填充为正方形而不是单独的点?
How can I make this a filled in square rather than seperate dots?
推荐答案
您需要根据数据点创建一个简单的多边形,并确保形状是封闭的(即第一个点必须与最后一个点相同).您将经度/纬度列转换为矩阵,并将其放在列表中以传递给 st_polygon
.
You need to create a simple polygon from the data points, ensuring that the shape is closed (i.e. the first point must be the same as the last point). You convert the longitude / latitude columns to a matrix and put it in a list to pass to st_polygon
.
创建多边形后,需要使用 st_sfc
将其转换为sfc:
Once you have created your polygon, you need to convert it to sfc using st_sfc
:
test <- as.matrix(rbind(test[,-1], test[1, -1]))
Coord_Ref <- st_crs(3035)
plot_locations_test <- st_polygon(x = list(test))
plot_locations_test <- st_sfc(plot_locations_test, crs = Coord_Ref)
ggplot(plot_locations_test) + geom_sf(fill = "red", alpha = 0.1)
数据
test <- structure(list(Shape = c(1.1, 1.1, 1.1, 1.1),
long = c(43L, 43L, 40L, 40L),
lat = c(10L, 13L, 13L, 10L)),
class = "data.frame",
row.names = c("1", "2", "3", "4"))
这篇关于将CSV文件转换为shapefile-但不希望多边形指向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!