How to get coordinates into data frame using over? - r

I have a SpatialPolygonDataFrame and a SpatialPointDataFrame. I want to extract the values of the polygons to the points. I'm using over()
values_poly = over(points, polygons)
What I get is a data.frame like described but how can I know which row was which point?
Is there a way?

Seems like someone else had more or less the same question.
For my purposes the function worked very well...
Joining point to polygon
Sorry for opening a new topic...
Greetings

Related

How to extract data points from rasterstack to a shapefile?

I need to extract daily weather data points from a raster stack and place it over a shapefile of various sewer sheds in my state. My boss wants me to use the writeRaster and extract function to do this. He said this would create a GeoTiff. I am new to R and unsure how to do something like this. Anything that can point me in the right direction would be appreciated.

Create Tesselation from SpatialPolygonsDataFrame?

Novice R programmer here... Looking for guidance on building a tess out of the polygons in a SpatialPolygonsDataFrame.
I am invoking quadratcount on points within a state boundary. Rather than using the default grid, I would like to use custom polygons to define the quadrats. Specifically, the county polygons which I have in shapefile format.
I take it from the documentation that the desired tesselation can be created out of a list of 'owin' objects. Where I'm getting jammed up is in taking my SpatialPolygonsDataFrame to generate that list.
I have confirmed that the polygons are read in correctly:
counties <- readOGR('/path/to/counties.shp', layer = "CountyBoundaries", GDAL1_integer64_policy = FALSE)
for(i in 1:nrow(counties)) {
plot(counties[i,])
}
Which generates a series of plots, one per county. That is, of course, only useful to know that my data isn't broken and that I can iterate over the polygons. What I think I need to do is make an owin out of each polygon in the SpatialPolygonsDataFrame and append that to myList for tess(tiles=myList). Not having much success in that approach.
My strong suspicion is that there's an easier way...
Many Thanks,
--gt
Turns out my problem was in not fully understanding how lists are indexed in R. The following bit of code gives me the result I want.
I have no doubt that there is a better, vectorized, way to do it. But in the mean while:
# The point events are in a PPP: StateCrimes_ppp
counties <- readOGR('/path/to/counties.shp', layer = "CountyBoundaries", GDAL1_integer64_policy = FALSE)
tlist <- list()
for(i in 1:nrow(counties)) {
tlist[[i]] <- as(counties[i,], 'owin')
}
QuadCount <- quadratcount(
StateCrimes_ppp,
tess=tess(tiles=tlist)
)
plot(QuadCount, main=NULL)
plot(intensity(QuadCount, image=TRUE), main=NULL, las=1)
If anybody sees how I've taken the long and hard way to solve a simple problem, I'd love to know a better, simpler, more elegant, or more R-like way to do this.
Thanks again,
--gt

How could I keep the data frame when working with shape files in R?

I'm working with two shapes and I want to get the first clean overlap with the second. To do this, I make the difference with rgeos :: gDifference () and it works correctly. The first shape is a SpatialPolygonsDataFrame and the second is a SpatialPolygons. As a result I need a SpatialPolygonsDataFrame, but I get a SpatialPolygon.
I have not yet found a way to correct this. Does anyone know a solution?
My script is:
ID <- as.character(shape_1#data$ID)
shape.gDiff <- gDifference(shape_1, shape_2, byid=T, id=ID)

Nearest line to points using R

I'm trying to do some GIS work using R. Specifically, I have a spatialpointsdataframe (called 'points') and a spatiallinesdataframe (called 'lines). I want to know the closest line to each point. I do this:
# make a new field to hold the line ID
points#data$nearest_line <- as.character('')
# Loop through data. For each point, get ID of nearest line and store it
for (i in 1:nrow(points)){
points#data[i,"nearest_line"] <-
lines[which.min(gDistance(points[i,], lines, byid= TRUE)),]#data$line_id
}
This works fine. My issue is the size of my data. I've 4.5m points, and about 100,000 lines. It's been running for about a day so far, and has only done 200,000 of the 4.5m points (despite a fairly powerful computer).
Is there something I can do to speed this up? For example if I was doing this in PostGIS I would add a spatial index, but this doesn't seem to be an option in R.
Or maybe I'm approaching this totally wrong?

Using R for extracing data from colour image

I have a scanned map from which i would like to extract the data into form of Long Lat and the corresponding value. Can anyone please tell me about how i can extract the data from the map. Is there any packages in R that would enable me to extract data from the scanned map. Unfortunately, i cannot find the person who made this map.
Thanks you very much for your time and help.
Take a look at OCR. I doubt you'll find anything for R, since R is primarily a statistical programming language.
You're better off with something like opencv
Once you find the appropriate OCR package, you will need to identify the x and y positions of your characters which you can then use to classify them as being on the x or y axis of your map.
This is not trivial, but good luck
Try this:
Read in the image file using the raster package
Use the locator() function to click on all the lat-long intersection points.
Use the locator data plus the lat-long data to create a table of lat-long to raster x-y coordinates
Fit a radial (x,y)->(r,theta) transformation to the data. You'll be assuming the projected latitude lines are circular which they seem to be very close to but not exact from some overlaying I tried earlier.
To sample from your image at a lat-long point, invert the transformation.
The next hard problem is trying to get from an image sample to the value of the thing being mapped. Maybe take a 5x5 grid of pixels and average, leaving out any gray pixels. Its even harder than that because some of the colours look like they are made from combining pixels of two different colours to make a new shade. Is this the best image you have?
I'm wondering what top-secret information has been blanked out from the top left corner. If it did say what the projection was that would help enormously.
Note you may be able to do a lot of the process online with mapwarper:
http://mapwarper.net
but I'm not sure if it can handle your map's projection.

Resources