[R-sig-Geo] Modifying a column in a Shapefile

Juliane Struve juliane_struve at yahoo.co.uk
Mon Oct 6 13:14:11 CEST 2008




Dear Roger,

thank you very much for your help, (and thank you to others who have helped me to set this up) . The code below seems to convert the data in column NEAR_ANGLE correctly and also writes a new shapefile,although I may have made mistakes that I am not yet aware of. 

MergedCentroids <- readOGR("Centroids_Merge.shp","Centroids_Merge")
MergedCentroids$NEARANG_R <- ifelse(MergedCentroids$NEAR_ANGLE<0,MergedCentroids$NEAR_ANGLE+360, MergedCentroids$NEAR_ANGLE)
MergedCentroids$NEARANG_R=90-MergedCentroids$NEARANG_R
MergedCentroids$NEARANG_R <- ifelse(MergedCentroids$NEARANG_R<0,MergedCentroids$NEARANG_R+360, MergedCentroids$NEARANG_R)
write.pointShape(coordinates(MergedCentroids),data.frame(MergedCentroids),paste("NEWMC")) 
NEWMCSPDF=readOGR("NEWMC.shp","NEWMC")

However:
#writeOGR(MergedCentroids, "NEWMCSPDF", driver="ESRI Shapefile")
did not work. I will try and find out more about it.

Regards,

Juliane 

Below is an example of my data.

coordinates                      Id PolyFID   UTMX    UTMY 
 (330430, 3043940)          0  187416 330430 3043937        
 (330430, 3044040)          0  187417 330430 3044037       
 (330430, 3044140)          0  187418 330430 3044137       
 (330430, 3044240)          0  187419 330430 3044237       
(330430, 3044340)             0  187420 330430 3044337     
(330430, 3044440)            0  187421 330430 3044437        
(330430, 3044540)              0  187422 330430 3044537        
 (330430, 3044640)              0  187423 330430 3044637       
 (330430, 3044740)              0  187424 330430 3044737       
 (330430, 3044840)              0  187425 330430 3044837        
 (330430, 3044940)              0  187426 330430 3044937        
 
    CELLTYPE NEAR_FID NEAR_DIST   NEAR_X  NEAR_Y NEAR_ANGLE NEARANG_R 
500        1              604         266.91098 330681.3 3043847  -19.66356                  109.6636    
501        1              604         314.96727 330681.3 3043847  -37.05982                  127.0598   
502        1              612         307.32628 330378.1 3044440   99.71783                  350.2822    
503        1              612         209.44248 330378.1 3044440  104.34049                  345.6595    
504        1              612         114.03715 330394.0 3044445  108.42658                  341.5734    
505        1              612         19.16420 330423.9 3044455  108.42658                     341.5734    
506        1              612          27.46970 330420.8 3044563  109.47166                  340.5283   
507        1              612          21.19512 330439.2 3044618  -64.29902                     154.2990    
508        1              612           111.30209 330478.3 3044637  -64.29902                  154.2990    
509        1              612         201.57582 330516.4 3044655  -64.61209                  154.6121    
510        1              612         291.91840 330555.2 3044673  -64.61209                  154.6121    


 
----- Original Message ----
From: Roger Bivand <Roger.Bivand at nhh.no>
To: Juliane Struve <juliane_struve at yahoo.co.uk>
Cc: r-sig-geo at stat.math.ethz.ch
Sent: Friday, 3 October, 2008 13:33:44
Subject: Re: [R-sig-Geo] Modifying a column in a Shapefile

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