[GIS] Checking if Lng and Lat fall inside polygons from ESRI Shapefile

point-in-polygonrshapefile

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.

Best Answer

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.

Related Question