Creating random polygons within a set shapefiles boundary in R

本小妞迷上赌 提交于 2019-12-10 21:36:15

问题


I have a shapefile which is the national boundary line of Chile. If possible, I would like to randomly generate e.g. 1000 polygons within this area. I'm wondering what the best way would be to go about this in R? I thought maybe, loading the shapefile using the 'shapefiles' package, then creating a point dataset using something available in 'spatstat'. Then I could create circular polygons based around these points... This is mostly hypothetical and I don't know if its possible.

I have some experience in R but haven't really tried any spatial analysis before.

Any thoughts and suggestions would be appreciated.


回答1:


Could you work with raster polygons?

Generate a fine grid (less than your target polygon area) over your shapefile polygon, then pick a random 'seed' grid square and 'grow' it by adding nearest grid cells incrementally, until it is the size you want. This way you'll get approximately circular polygons inland, and semi-circular polygons if you start with a coastal cell.

Whether a raster representation is okay for you depends on what you plan to do when you have created these polygons, but it looks like you are just going to overlay them on other land use data, which can be done with rasters on rasters or rasters on polygons.

Should all be doable within the raster package.




回答2:


This is a bit of a hard problem really since you need to worry about some things. Polygons can be simple convex things, or more complicated "concave" shapes, or multi-branched shapes composed of multiple "islands" and lakes. I would get that part straight first since if you only need the simple case it's better.

1) Convex polygons are easy to generate from random points, since you just need the convex hull - the ordering of the points does not affect the result. For actual polygons the order in which you link them together as a boundary is very important or you will get non-sensical shapes with twisted boundaries. But, there is the alphahull package to generate smarter hulls from non-convex points.

2) If no part of the polygon can exceed the main boundary, then you need every vertex to be both inside the boundary and that no segment joining them crosses the main boundary. Imagine a polygon whose vertices are all inside the national boundary, but one segment crosses a river inlet. There are sampling functions for getting points within a polygon (spsample in sp, csr in splancs) and there are geometry tests in rgeos that could be used to ensure there are no crossings - still that's extra work since you are testing for failure of a set of sample points defining a polygon rather than generating known "good cases".

If you can relax some of the constraints then it might be fairly doable. You mention "circular polygons", do you really mean circles (or approximate circles) since if so then some of the tests could become much simpler.

I'd be inclined to dig into your aims and see if this really is what you want to be doing, or if you can simplify things more.



来源:https://stackoverflow.com/questions/9989508/creating-random-polygons-within-a-set-shapefiles-boundary-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!