title: R- Calculating the minimum distance between points and lines
body: <p>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. </p>
  
  <p>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.</p>
  
  <p>For the point2line, my code looked something like this: </p>
  
  <pre><code>rail_projection &lt;-spTransform(rail_file,CRS(proj4string(parcels)) #project to same CRS
  rail_crop&lt;-crop(rail_projection,extent(parcels)) #crop to extent of parcels
  centroids&lt;-gCentroid(parcels,byid=T) # take centroid of parcel polygons.
  m&lt;-gDistance(rail_crop,centroids,byid=T)
  </code></pre>
  
  <p>but this spits out the following warning:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>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.</p>
  
  <p>The second method, using dist2line looks like this:
      dist&lt;-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.</p>
  
  <p>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)</p>
  
  <p>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. </p>
  
  <p>Thanks!</p>
  
  <h2>Edit with Data</h2>
  
  <p>So I guess I figured out where my warning message was coming from using <a href="https://gis.stackexchange.com/questions/148852/gdistance-to-determine-closest-point-to-polygon-does-changing-projection-change">this</a>. 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 <a href="https://ufile.io/6mpyz" rel="nofollow noreferrer">here</a></p>