[R-sig-Geo] Modifying a column in a Shapefile
Roger Bivand
Roger.Bivand at nhh.no
Fri Oct 3 14:33:44 CEST 2008
On Fri, 3 Oct 2008, Juliane Struve wrote:
> Dear list members,
>
> I am using a grid called "MergedCentroids" in which I need to
> modify entries in column "NEARANGLE and write the result into a
> column "NEARANG_R". I have tried to convert the Spatial Points Data
> Frame to a data frame in order to do this. I want to convert the updated
> grid into a new shapefile at the end. This is my code so far:
>
> MergedCentroids <- readOGR("Centroids_Merge.shp","Centroids_Merge")
> write.table(MergedCentroids,file="MergedCentroids.csv",sep=",",row.names=F)
> MC=read.csv("MergedCentroids.csv")
> MCDF<-as.data.frame(MC)
> MCDF$NEARANG_R=ifelse(MCDF$NEARANGLE<0,MCDF$NEARANGLE+360,MCDF$NEARANG)
> MCSPDF = SpatialPointsDataFrame(MCDF[,3:4],data.frame(MCDF[,1:16]))
> write.pointShape(coordinates(MCSPDF),data.frame(MCSPDF),paste("NEWMCSPDF"))
>
> After executing the last statement I get two error messages:
>
> write.pointShape(coordinates(MCSPDF),data.frame(MCSPDF),paste("NEWMCSPDF"))
> Error in write.dbf(df, paste(file, ".dbf", sep = ""), factor2char = factor2char, :
> unable to open file
> In addition: Warning message:
> use writeSpatialShape:
> objects other than Spatial objects defined in the sp package are deprecated
>
> Would somebody be able to help ?
Difficult to know where to start. If you had used a provided data set, it
might have been easier.
First, to coerce to a data.frame, use as(), the coerce method:
MC <- as(MergedCentroids, "data.frame")
What actually got written to the CSV file probably resulted from coercion
being invoked internally in write.table() - did you inspect the file?
Why not just operate on the MergedCentroids object?
MergedCentroids$NEARANG_R <- ifelse(MergedCentroids$NEARANGLE<0,
MergedCentroids$NEARANGLE+360, MergedCentroids$NEARANG)
is quite OK. Why not then simply use:
writeOGR(MergedCentroids, "NEWMCSPDF", driver="ESRI Shapefile")
I can imagine that the input object is polygons not points, adding one
possible extra level, and that the OGR driver does not handle dates
gracefully, leading you to prefer writeSpatialShape() in maptools (using
the improved write.dbf() in foreign), but not much more reason for the
complications you introduce. Simplifying usually helps, as does giving
working examples from the included data sets.
Roger
>
> Many thanks !
>
> Juliane
>
>
> Dr. Juliane Struve
> Environmental Scientist
> 10, Lynwood Crescent
> Sunningdale SL5 0BL
> 01344 620811
>
>
>
>
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
--
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, Helleveien 30, N-5045 Bergen,
Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: Roger.Bivand at nhh.no
More information about the R-sig-Geo
mailing list