Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I have two input files.

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

Second: ESRI Shape file 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 somesearch, and I have found that I can use "MapTools" package in R to load the ESRI shape file.. 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 shape file 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.

share|improve this question
add comment

2 Answers 2

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.

share|improve this answer
    
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 –  Moustafa Alzantot Jun 27 '13 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 ? –  Moustafa Alzantot Jun 27 '13 at 14:34
    
@MoustafaAlzantot What's the exact command you're using to import data using readOGR? –  Roman Luštrik Jun 28 '13 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 –  Moustafa Alzantot Jun 28 '13 at 21:51
    
@MoustafaAlzantot please edit your original question, not everyone reads comments. Plus, you have greater flexibility regarding code folding and syntax highlighting. –  Roman Luštrik Jun 29 '13 at 11:37
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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