Dear list,

I was trying to write a shapefile within a small function (to calculate
centroids to polygons) but not able to do so
My code was


x<-readOGR(dsn="C:/.....", layer="x")

get.centroids<-function(x) {
x.polyset<-SpatialPolygons2PolySet(x)
centroids <- calcCentroid(x.polyset, rollup=2)
d.crs<-CRS(toString(x@proj4string@projargs))
centShape<-SpatialPoints(cbind(x=centroids$X,y=centroids$Y),proj4string=d.crs).
# plot the poly as well as centroids.
plot(x)
points(centShape,pch=18)
print(str(centShape))
tmpdir<-("C:/........./")
writeSpatialShape(centShape,paste(tmpdir,"x_","target_vill_centroid.shp",
sep=""))
} # This function do not write shapefile to directory.


I am able to plot the centroids and polygons but not able to write it to a
file.

Also, is it possible to access the path of "x" from the SpatialDataFrame??
to remove the need to add "tmpdir" in above code??
I could not find any slot mentioning the path of "x"

My sessioninfo is

R version 2.15.2 (2012-10-26)
Platform: i386-w64-mingw32/i386 (32-bit)

locale:
[1] LC_COLLATE=English_India.1252  LC_CTYPE=English_India.1252
[3] LC_MONETARY=English_India.1252 LC_NUMERIC=C
[5] LC_TIME=English_India.1252

attached base packages:
[1] grid      stats     graphics  grDevices utils     datasets  methods
[8] base

other attached packages:
[1] maptools_0.8-22    lattice_0.20-10    foreign_0.8-51
[4] PBSmapping_2.65.40 rgdal_0.8-4        sp_1.0-5

loaded via a namespace (and not attached):
[1] tools_2.15.2


Thanks,

Vikram.

	[[alternative HTML version deleted]]

