To export Spatial Data Frame of GWR results from R do I need to use readShapeSpatial?

df.bg <- as.data.frame(bg)
df.bg.spdf <- SpatialPolygonsDataFrame(bg, df.bg)

gwr.res1 <- gwr.basic(bg$DIVINDX10 ~ bg$SOLAR_AREA + bg$Avg_NO2_co,

  • Shapefile is but one vector-map format, and not a very good one - there are many others. gdal.org/ogr_formats.html Use rgdal::writeOGR for the more general case, or convert to simple features with sf::st_as_sf(x) and use sf::st_write. – mdsumner Dec 17 '16 at 1:58

There are write functions in maptools, which is the package that I assuming you are using since you did not provide this information, including the exact corollary, "writeSpatialShape". However, I prefer using the rgdal package for reading and writing spatial vector data. The "readOGR" and "writeOGR" functions are the workhorses for handling vector data and they retain the projection information. There is also the "shapefile" function in the raster package that facilitates both read and write in the same function. It is just a wrapper for the OGR functions in rgdal but provides a bit simpler in syntax and a single funciton.

An example with your data would be:

# rgdal
writeOGR(gwr.res1$SDF, getwd(), "gwr_polys", driver="ESRI Shapefile")      

# raster
shapefile(gwr.res1$SDF, "gwr_polys")

It is important that you: 1) include the R packages that you are using and 2) make an attempt to provide a reproducible example. If you want code syntax highlighting, highlight your code and click the {} button in the menu bar.


Since you are using maptools to read the data, the appropriate export function is writeSpatialShape:


bg <- readShapeSpatial("BG_Div_NO2_Solar_Nozero.shp")    
gwr.res1 <- gwr.basic(DIVINDX10 ~ SOLAR_AREA + Avg_NO2_co,
writeSpatialShape(gwr.res1$SDF, "gwr_result")

Some notes:

  1. readShapeSpatial returns a S*DF object, so there is no need to extract the data frame and then rejoin it to the SpatialPolygons. I have eliminated those lines from the code.
  2. When you specify the data frame in the data parameter, you don't need to qualify the variable names in the formula (dropped bg$ prefix above).
  3. Many of us prefer to use rgdal rather than maptools because rgdal reads/writes to a wide variety of spatial data formats, while maptools only reads/writes shapefiles. (See Jeffrey Evans' answer to your question.)
  • DONT use readShapeSpatial - it ignores the projection file. The only reason people seem to use it is because its easier to construct a path to the file than use readOGR and supply the path and the shapefile name - that excuse is removed if you use raster::shapefile which takes the path, splits it for you, and uses readOGR, resulting in a properly read spatial object with projection info. /slightrant – Spacedman Dec 20 '16 at 8:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.