1
votes

I have a large set of polygon shapefiles of parcels (for 50+ counties) and I would like to loop through each of them and calculate the distance from the center of each parcel to the nearest railroad, stored nationally in a line shapefile.

I had come up with two different measures to do this: point2Line from the geospheres package and gDistance from rgeos package. Each had different problems for me.

For the point2line, my code looked something like this:

rail_projection <-spTransform(rail_file,CRS(proj4string(parcels)) #project to same CRS
rail_crop<-crop(rail_projection,extent(parcels)) #crop to extent of parcels
centroids<-gCentroid(parcels,byid=T) # take centroid of parcel polygons.
m<-gDistance(rail_crop,centroids,byid=T)

but this spits out the following warning:

Warning messages:
1: In RGEOSDistanceFunc(rail_crop, centroids, byid=T, "rgeos_distance") :
Spatial object 1 is not projected; GEOS expects planar coordinates
2: In RGEOSDistanceFunc(rail_crop, centroids, byid=T, "rgeos_distance") :
Spatial object 2 is not projected; GEOS expects planar coordinates

It also gives me a nparcels X (I think number of line matrix) of numbers for which I'm not quite sure of the measurement. I'd like the measurement in KM or miles or something.

The second method, using dist2line looks like this: dist<-dist2Line(centroids,rail_crop) It works fine, but the only problem is it takes very long! For looping through 60 or so counties, it may take a couple days. Additionally, I'd rather not have to crop the rail shapefile in case the closest railroad is in another county. Running the code with the entire shapefile takes wayyy longer.

So what I'm essentially look for is a way to get nearest distance from points to a line shapefile in a fairly efficient way that spits out the distance in km or miles, or something similarly interpretable. If this can be done with gDistance in a way that doesn't have these errors then great! If there's a way to speed it up from dist2Line, or some other method, that'd also be great. If you also have any thoughts on how to crop the rail shapefile with some buffer, I'd also appreciate it. (I can't post code since the parcel data is proprietary)

I'm still a beginner with spatial stuff so sorry if I have trouble following or if this has been answered before. I've looked around and haven't found solutions I'm able to make work here.

Thanks!

Edit with Data

So I guess I figured out where my warning message was coming from using this. I was using a 3d coordinate system rather than a planar one. I've run some code with a shapefile I downloaded from the internet for a county in Wisconsin, and gDistance spits out a matrix with the number of parcels x the number of lines with distances (I presume) from each parcel to each rail line. Does anyone know in what measurement these distances come out? How can I convert them to KM or miles? Also is there a "preferred" planar projection I should be using. Furthermore, any thoughts on how to crop the shapefile with some buffer would be greatly appreciated. Files/code can be found here

1
You should post some working code. Provide some simple example data created with code; there are numerous examples that do so.Robert Hijmans
@RobertH added some code/clarified a bit after figuring a few things out. The last section of my edited post should explain my key questionswinitheju

1 Answers

1
votes

I would recommend looking into the Universal Transverse Mercator system. I don't know if it's the best projected coordinate system, but it's the one I see used most frequently in my field (as long as the study area is not too large).

Once you find the UTM zone specific to your region, you can use sp::spTransform() to reproject your shapefiles such that gDistance() can return your distances in either kilometers or meters.

Here's the proj4string for a UTM zone that covers Kenya: +proj=utm +zone=37 +south +ellps=WGS84 +datum=WGS84 +units=m +no_defs. As you can see, the units here are in meters (+units=m).

It might be worth your time to look into the relatively new sf package, which simplifies and standardizes functions and classes for spatial data. Here's a brief intro you can check out: http://strimas.com/r/tidy-sf/. The analogous functions from that package are st_transform() and st_distance().