6

I have two input files.

First: CSV file with a list of points each defined with Lng, Lat, Name

Second: ESRI Shapefile defining a number of polygons

I would like to determine for each point I have (from the CSV file) which polygon does it fall inside (if any)?

I will be using R.

I know that this might be a straightforward task but I am totally a newbie to GIS.

I have done some searching, and I have found that I can use "MapTools" package in R to load the ESRI shapefile, but I don't know how to move after that.

I managed to read the shapefile using readShapePoly function in the MapTools package and now I can plot the polygons and iterate through them in R. I also loaded the points from the CSV file into a data frame with two columns (lng, lat). Now I need to check for each point which polygon does it fall inside (if any)...

I managed to do this using the function over from package sp in the way explained here, however I am getting NA result for all points which is unexpected. I think it is due to different projections for the points and polygons.. Examining the polygons loaded from the shapefile using summary function gives me Is projected: NA, proj4string : [NA]

Any tips ?

Any advice, good tutorial or code sample showing how to do that would be highly appreciated.

2 Answers 2

3

You will want to import your shape file. You can try package rgdal, specifically function readOGR.

After reading into R your csv file, you may want to coerce it to a SpatialPoints class. Make sure your projections are identical.

After you have the polygon and SpatialPoints, it should be just a matter of using function over from package sp.

I recently asked a similar question. I hope the data in the self contained example should be sufficient to work through the second part of your question.

5
  • I tried many times to use the readOGR function but it keeps giving me "unable to open file".. although the file exists.. I managed to read the shapefile using readShapePoly function in the MapTools package.. and Now I can plot the polygons and iterate through them in R. I also loaded the points from the CSV file into a data frame with two columns (lng, lat).. Now I need to check for each point which polygon does it fall inside (if any) .. any help about that Commented Jun 27, 2013 at 14:07
  • I managed to use the function over' from package sp` in the way explained in your answer link However I am getting NA result for all points which is unexpected. I think it is due to different projections for the points and polygons.. Examining the polygons loaded from the shape file using summary function gives me Is projected: NA, proj4string : [NA] Any tips ? Commented Jun 27, 2013 at 14:34
  • @MoustafaAlzantot What's the exact command you're using to import data using readOGR? Commented Jun 28, 2013 at 18:31
  • it is readOGR(dsn=".", layer="shape_file_name_without_.shp") The problem is solved now, after using spTransform to transform the SpatialPoints into the same projection as the Polygons using spTransform function Commented Jun 28, 2013 at 21:51
  • @MoustafaAlzantot please edit your original question, not everyone reads comments. Plus, you have greater flexibility regarding code folding and syntax highlighting. Commented Jun 29, 2013 at 11:37
-2

I recently did this in Python, utilizing the Ray Casting algorithm. The function "draws" an arbitrary ray from the point in any direction and "counts" how many times the ray crosses the edge of the polygon: if the number is odd, the point lies within the polygon, if odd, it is outside.

I don't have any experience with R, but I found this link on pnt.in.poly. It utilizes a different algorithm which uses the sum of the angles made between the point in question and the vertices of the polygon. If you can't use that for whatever reason, you might want to research the ray casting algorithm above. There are plenty code examples in various languages documenting it.

Regardless of which method you go with (or another one entirely), you'll obviously need an array of the vertices of the polygon.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.