In many instances data is available only in the form of shapefiles. A shapefile is a simple format for storing geometric location and attribute information of geographic features. The shapefile is a popular geospatial vector format developed and regulated by ESRI as a (mostly) open specification. Geographic features in a shapefile can be represented by points, lines, or polygons (areas).
“R” contains various functions to read and write shapefiles. Here we show how to convert the new 2462 ft contour data from a standard text format to a shapefile.
The following are the first few lines from a text file of the contour values of Deep Creek Lake, MD, Eastings and Northings in ft.
Easting Northing 649177.015 698016.962 649182.756 698030.201 649204.067 698060.465 649222.207 698077.754
A shapefile needs a CRS specification (Coordinate Reference System). I extracted that from an existing shapefile, namely the one provided to me by the County in 2012 and plotted the data.
Here is the code for it:
shp1file <- "../data/dcl_outline_carpenter/DCLShoreline.shp" shp1 <- readOGR(shp1file, "DCLShoreline") plot(shp1, col="red", main="Deep Creek Lake", lwd=1)
The plot is shown in Figure 1.
Figure 1. The Outline Extracted from the Old Shoreline Shapefile.
The CRS string extracted from this file looks like this:
+proj=lcc +lat_1=38.3 +lat_2=39.45 +lat_0=37.66666666666666 +lon_0=-77
After having read in the text file with the Northings and Easting data, the new shapefile is written by the following code snippet:
# The use a shapefile function we must first transform the data into # a spatial points dataframe # Note that the easting and northing columns are in columns 1 and 2 plot.df1 <- SpatialPointsDataFrame(df1[,1:2], # The columns to use df1, # The R data object to convert proj4string = prjstr) # Assign a CRS (extracted above) # Plot the data to check if OK (blue color) plot(plot.df1, col="blue", cex=0.1) # Double check to see of the CRS has been transferred properly crs(plot.df1) # Export the shapefile writeOGR(plot.df1, "../results/dcl_shoreline_2462", "century2462-2017", driver="ESRI Shapefile")
To check that all is done well, we read in the shapefile and plot it. The result is shown in Figure 2.
Figure 2. The New, 2017, Shoreline Data Written and Reread from a Shapefile.
To conclude… The conversion worked successfully.
PLV: First Published: 9/14/2017