[R-sig-Geo] Error in converting SpatialPointsDataFrame object to ESRI shape file using writeOGR from rgdal package

Roger Bivand Roger.Bivand at nhh.no
Tue Jun 17 06:02:48 CEST 2008


On Mon, 16 Jun 2008, Debarchana Ghosh wrote:

> Hi,
>
> I am using R 2.7 on windows with 4GB ram.
>
> I am trying to convert the SpatialPointsDataFrame object (SDF) from the
> "gwr" output ( spgwr package) to ESRI shapefile using the writeOGR function
> from the rgdal package. The function is able to write the shape file with
> correct geometry but showing errors when trying to view the attribute table.
> The attribute table is empty. But the data slot of the
> SpatialPointsDataFrame object has values for all the columns. Below I paste
> my codes:

The issue seems to be that the underlying DBF format cannot handle field 
names which are longer than 11 characters. I've looked at:

names(test500.gwr$SDF) <- make.names(names(test500.gwr$SDF))

and tried abbreviate() too, and toupper() without much luck. Maybe:

toupper(gsub("\\.", "_", make.names(substring(names(test500.gwr$SDF), 1,
   7), unique=TRUE)))

or even less than 7 if need be?

What else apart from shapefiles can Arc read that writeOGR can write that 
would let us get round using DBF?

Roger

>
> # packages spgwr, maptool, sp, foreign
> # Finding the bandwidth by the AIC method
> test500.bw.aic<-gwr.sel(yimp00~ english + slope30 + mac95d + mdinc_t +
> musa00
>  + popden_t + dissewer + dismcd + diswater + ag04 + c1dishwy + c1dipark +
> tccost1 + soil1 + soil2,
>  data=base500vars, coords = cbind(base500vars$longx, base500vars$laty),
> method = "aic", longlat=TRUE)
>
> # gauss gwr function
> test500.gwr<-gwr(yimp00~ english + slope30 + mac95d + mdinc_t + musa00 +
> popden_t + dissewer + dismcd + diswater
>  + ag04 + c1dishwy + c1dipark + tccost1 + soil1 + soil2, data=base500vars,
>  coords = cbind(base500vars$longx, base500vars$laty), longlat=TRUE,
> bandwidth = test500.bw.aic,
>  hatmatrix=TRUE, se.fit=TRUE)
>
> # output of gwr
> names(test500.gwr)
> [1] "SDF"       "lhat"      "lm"        "results"   "bandwidth" "adapt"
> "hatmatrix" "gweight"   "this.call"
>
> class(test500.gwr$SDF)
> [1] "SpatialPointsDataFrame"
> attr(,"package")
> [1] "sp"
>
>> names(test500.gwr$SDF)
> [1] "sum.w"          "(Intercept)"    "english"        "slope30"
> "mac95d"         "mdinc_t"        "musa00"         "popden_t"
> [9] "dissewer"       "dismcd"         "diswater"       "ag04"
> "c1dishwy"       "c1dipark"       "tccost1"        "soil1"
> [17] "soil2"          "R2"             "gwr.e"          "(Intercept)_se"
> "english_se"     "slope30_se"     "mac95d_se"      "mdinc_t_se"
> [25] "musa00_se"      "popden_t_se"    "dissewer_se"    "dismcd_se"
> "diswater_se"    "ag04_se"        "c1dishwy_se"    "c1dipark_se"
> [33] "tccost1_se"     "soil1_se"       "soil2_se"
>
> # I changed the names of column 2 and 20 to "Intercept" and "Intercept_se"
> because ESRI shapefiles does not like brackets
>> names(test500.gwr$SDF)[2]<-"Intercept"
>> names(test500.gwr$SDF)[20]<-"Intercept_se"
>> names(test500.gwr$SDF)
> [1] "sum.w"        "Intercept"    "english"      "slope30"
> "mac95d"       "mdinc_t"      "musa00"       "popden_t"     "dissewer"
> [10] "dismcd"       "diswater"     "ag04"         "c1dishwy"
> "c1dipark"     "tccost1"      "soil1"        "soil2"        "R2"
> [19] "gwr.e"        "Intercept_se" "english_se"   "slope30_se"
> "mac95d_se"    "mdinc_t_se"   "musa00_se"    "popden_t_se"  "dissewer_se"
> [28] "dismcd_se"    "diswater_se"  "ag04_se"      "c1dishwy_se"
> "c1dipark_se"  "tccost1_se"   "soil1_se"     "soil2_se"
>
> # Converting a "SpatialPointsDataFrame" object to ESRI shape file
> # packages rgdal
> writeOGR(test500.gwr$SDF, place, "test500gwr", driver="ESRI Shapefile")
>
> I cannot seem to find the error. Is there any other way to write the
> 'SpatialPointsDataFrame' object to ESRI shapefiles?
>
> Any pointers will be very helpful.
>
>

-- 
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