Skip to content

Instantly share code, notes, and snippets.

@anarosner
Created October 9, 2014 19:56

Revisions

  1. anarosner created this gist Oct 9, 2014.
    19 changes: 19 additions & 0 deletions import_export_shapefiles
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    #code snippet to import/export spatial files to/from r

    library(maptools)
    library(rgdal)

    # go to http://spatialreference.org/ to find proj4 string if you don't have it


    #for importing/exporting esri shapefiles
    r_object <- readShapePoly( fn="shapefile_name", proj4string=CRS("sdfsdlfj") )
    r_object$new_col <- r_object$old_col*10
    writeOGR(r_object, ".", layer="new_shapefile_name", driver="ESRI Shapefile")


    #for exporting geojsons (from imported esri shapefile)
    #example for Chris
    boo <- readShapePoly( fn="huc4", IDvar="HUC4", proj4string=CRS("sdfsdlfj") )
    #(fn is short for filename, not function)
    writeOGR(boo, "huc.geojson", layer="huc", driver="GeoJSON")