merging several shapefiles into one

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

merging several shapefiles into one

Nicolas Degallier
Hi All,

I have read different shapefiles that are contiguous and that have different column names with the function "readShapePoly".

example :

noumea <- readShapePoly(noumea.file, IDvar = "NOUMEA2_ID", proj4string = crs_rgnc)

and

dumbea <- readShapePoly(dumbea.file, IDvar = "DUMBEA2_", proj4string = crs_rgnc)

Now I would like to create a new shapefile with all the polygones in the "noumea" and "dumbea" spatial polygone data frames.

I tried :
spRbind(noumea, dumbea)

I get this message error :

Error in spRbind(as(obj, "SpatialPolygons"), as(x, "SpatialPolygons")) :
 non-unique polygon IDs

I have tried with the function rbind()
I have tried to set noumea$NOUMEA2_ID and dumbea$DUMBEA2_ with unique values but I still get the same message error as if the function spRbind was not using those two columns as polygons ID...

Does anyone know how I can fix this problem ?

Thanks a lot

Nicolas


Nicolas Degallier

IRD UMR182
Laboratoire d'Océanographie et du Climat, Expérimentation et Approches Numériques (LOCEAN)
Tour 45-55, 4e ét., case 100, 4 place Jussieu
75252  Paris Cedex 5  France
tél: (33) 01 44 27 51 57
fax: (33) 01 44 27 38 05

E-mail: <[hidden email]>
pdf reprints:
http://www.locean-ipsl.upmc.fr/~ndelod/production/

Skype:"xuxaxu"

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: merging several shapefiles into one

Pierre Roudier
Hi Nicolas,

I do not have a R session handy, but if I remember correctly, the
function gUnion() in the rgeos package would do what you want.

Hope this helps,

Pierre

2011/5/25 Nicolas Degallier <[hidden email]>:

> Hi All,
>
> I have read different shapefiles that are contiguous and that have different column names with the function "readShapePoly".
>
> example :
>
> noumea <- readShapePoly(noumea.file, IDvar = "NOUMEA2_ID", proj4string = crs_rgnc)
>
> and
>
> dumbea <- readShapePoly(dumbea.file, IDvar = "DUMBEA2_", proj4string = crs_rgnc)
>
> Now I would like to create a new shapefile with all the polygones in the "noumea" and "dumbea" spatial polygone data frames.
>
> I tried :
> spRbind(noumea, dumbea)
>
> I get this message error :
>
> Error in spRbind(as(obj, "SpatialPolygons"), as(x, "SpatialPolygons")) :
>  non-unique polygon IDs
>
> I have tried with the function rbind()
> I have tried to set noumea$NOUMEA2_ID and dumbea$DUMBEA2_ with unique values but I still get the same message error as if the function spRbind was not using those two columns as polygons ID...
>
> Does anyone know how I can fix this problem ?
>
> Thanks a lot
>
> Nicolas
>
>
> Nicolas Degallier
>
> IRD UMR182
> Laboratoire d'Océanographie et du Climat, Expérimentation et Approches Numériques (LOCEAN)
> Tour 45-55, 4e ét., case 100, 4 place Jussieu
> 75252  Paris Cedex 5  France
> tél: (33) 01 44 27 51 57
> fax: (33) 01 44 27 38 05
>
> E-mail: <[hidden email]>
> pdf reprints:
> http://www.locean-ipsl.upmc.fr/~ndelod/production/
>
> Skype:"xuxaxu"
>
> _______________________________________________
> R-sig-Geo mailing list
> [hidden email]
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>



--
Scientist
Landcare Research, New Zealand

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: merging several shapefiles into one

Barry Rowlingson
In reply to this post by Nicolas Degallier
On Wed, May 25, 2011 at 8:06 AM, Nicolas Degallier
<[hidden email]> wrote:

> Hi All,
>
> I have read different shapefiles that are contiguous and that have different column names with the function "readShapePoly".
>
> example :
>
> noumea <- readShapePoly(noumea.file, IDvar = "NOUMEA2_ID", proj4string = crs_rgnc)
>
> and
>
> dumbea <- readShapePoly(dumbea.file, IDvar = "DUMBEA2_", proj4string = crs_rgnc)
>
> Now I would like to create a new shapefile with all the polygones in the "noumea" and "dumbea" spatial polygone data frames.
>
> I tried :
> spRbind(noumea, dumbea)
>
> I get this message error :
>
> Error in spRbind(as(obj, "SpatialPolygons"), as(x, "SpatialPolygons")) :
>  non-unique polygon IDs
>
> I have tried with the function rbind()
> I have tried to set noumea$NOUMEA2_ID and dumbea$DUMBEA2_ with unique values but I still get the same message error as if the function spRbind was not using those two columns as polygons ID...
>
> Does anyone know how I can fix this problem ?

 once you've read in your data using NOUMEA2_ID and DUMBEA2_ as the ID
variables, changing the values in the data frame won''t change the ID
attached to each feature.

 You need to use the "spChFIDs-methods" to create new spatial polygons
data frames with unique IDs between them.

 Here, scot1 is the first 10 features from scot_BNG as read in help(rgdal)

 > scot2=scot1
 > scot12=spRbind(scot1,scot2)
 Error in spRbind(as(obj, "SpatialPolygons"), as(x, "SpatialPolygons")) :
  non-unique polygon IDs

 - that fails because the IDs are the same. So we change scot2 to be
"A" to "J" (which isnt what the IDs are currently...)

 > scot2=spChFIDs(scot2,letters[1:10])
 > scot12=spRbind(scot1,scot2)

and that works.

Barry

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: merging several shapefiles into one

zia207
In reply to this post by Pierre Roudier
I used following code to merge a series of  shape files with similar file structure. We can try this, but you need to do some modification of the code.
Zia

###############################################################################

# Require packages: rgdal and maptool
#-------------------------------------

library(rgdal)
library(maptools)

# Get list of all shape files from county directories/sptial/
#------------------------------------------------------------

files <- list.files(pattern="soilmu_a_ia.*.shp$", recursive=TRUE,full.names=TRUE)
uid<-1

# Get polygons from first file (_001)
#-------------------------------------

poly.data<- readOGR(files[1],gsub("^.*/(.*).shp$", "\\1", files[1]))
n <- length(slot(poly.data, "polygons"))
poly.data <- spChFIDs(poly.data, as.character(uid:(uid+n-1)))
uid <- uid + n

# mapunit polygoan: combin remaining  polygons with first polygoan
#-----------------------------------------------------------------

for (i in 2:length(files)) {
    temp.data <- readOGR(files[i], gsub("^.*/(.*).shp$", "\\1",files[i]))
    n <- length(slot(temp.data, "polygons"))
    temp.data <- spChFIDs(temp.data, as.character(uid:(uid+n-1)))
    uid <- uid + n
    poly.data <- spRbind(poly.data,temp.data)
}

names(poly.data)
proj4string(poly.data)


On 5/25/2011 3:22 AM, Pierre Roudier wrote:
Hi Nicolas,

I do not have a R session handy, but if I remember correctly, the
function gUnion() in the rgeos package would do what you want.

Hope this helps,

Pierre

2011/5/25 Nicolas Degallier [hidden email]:
Hi All,

I have read different shapefiles that are contiguous and that have different column names with the function "readShapePoly".

example :

noumea <- readShapePoly(noumea.file, IDvar = "NOUMEA2_ID", proj4string = crs_rgnc)

and

dumbea <- readShapePoly(dumbea.file, IDvar = "DUMBEA2_", proj4string = crs_rgnc)

Now I would like to create a new shapefile with all the polygones in the "noumea" and "dumbea" spatial polygone data frames.

I tried :
spRbind(noumea, dumbea)

I get this message error :

Error in spRbind(as(obj, "SpatialPolygons"), as(x, "SpatialPolygons")) :
 non-unique polygon IDs

I have tried with the function rbind()
I have tried to set noumea$NOUMEA2_ID and dumbea$DUMBEA2_ with unique values but I still get the same message error as if the function spRbind was not using those two columns as polygons ID...

Does anyone know how I can fix this problem ?

Thanks a lot

Nicolas


Nicolas Degallier

IRD UMR182
Laboratoire d'Océanographie et du Climat, Expérimentation et Approches Numériques (LOCEAN)
Tour 45-55, 4e ét., case 100, 4 place Jussieu
75252  Paris Cedex 5  France
tél: (33) 01 44 27 51 57
fax: (33) 01 44 27 38 05

E-mail: [hidden email]
pdf reprints:
http://www.locean-ipsl.upmc.fr/~ndelod/production/

Skype:"xuxaxu"

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo




_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo

zua3.vcf (294 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: merging several shapefiles into one

Mathieu Rajerison
I haven't tested it but also, using the Reduce function may work to apply
spRbind function across different Spatial*DataFrame objects contained in a
list:

spMerged<-Reduce(rbind,listSp)


2011/5/25 Zia Ahmed <[hidden email]>

>  I used following code to merge a series of  shape files with similar file
> structure. We can try this, but you need to do some modification of the
> code.
> Zia
>
>
> ###############################################################################
>
> # Require packages: rgdal and maptool
> #-------------------------------------
>
> library(rgdal)
> library(maptools)
>
> # Get list of all shape files from county directories/sptial/
> #------------------------------------------------------------
>
> files <- list.files(pattern="soilmu_a_ia.*.shp$",
> recursive=TRUE,full.names=TRUE)
> uid<-1
>
> # Get polygons from first file (_001)
> #-------------------------------------
>
> poly.data<- readOGR(files[1],gsub("^.*/(.*).shp$", "\\1", files[1]))
> n <- length(slot(poly.data, "polygons"))
> poly.data <- spChFIDs(poly.data, as.character(uid:(uid+n-1)))
> uid <- uid + n
>
> # mapunit polygoan: combin remaining  polygons with first polygoan
> #-----------------------------------------------------------------
>
> for (i in 2:length(files)) {
>     temp.data <- readOGR(files[i], gsub("^.*/(.*).shp$", "\\1",files[i]))
>     n <- length(slot(temp.data, "polygons"))
>     temp.data <- spChFIDs(temp.data, as.character(uid:(uid+n-1)))
>     uid <- uid + n
>     poly.data <- spRbind(poly.data,temp.data)
> }
>
> names(poly.data)
> proj4string(poly.data)
>
>
> On 5/25/2011 3:22 AM, Pierre Roudier wrote:
>
> Hi Nicolas,
>
> I do not have a R session handy, but if I remember correctly, the
> function gUnion() in the rgeos package would do what you want.
>
> Hope this helps,
>
> Pierre
>
> 2011/5/25 Nicolas Degallier <[hidden email]> <[hidden email]>:
>
>  Hi All,
>
> I have read different shapefiles that are contiguous and that have different column names with the function "readShapePoly".
>
> example :
>
> noumea <- readShapePoly(noumea.file, IDvar = "NOUMEA2_ID", proj4string = crs_rgnc)
>
> and
>
> dumbea <- readShapePoly(dumbea.file, IDvar = "DUMBEA2_", proj4string = crs_rgnc)
>
> Now I would like to create a new shapefile with all the polygones in the "noumea" and "dumbea" spatial polygone data frames.
>
> I tried :
> spRbind(noumea, dumbea)
>
> I get this message error :
>
> Error in spRbind(as(obj, "SpatialPolygons"), as(x, "SpatialPolygons")) :
>  non-unique polygon IDs
>
> I have tried with the function rbind()
> I have tried to set noumea$NOUMEA2_ID and dumbea$DUMBEA2_ with unique values but I still get the same message error as if the function spRbind was not using those two columns as polygons ID...
>
> Does anyone know how I can fix this problem ?
>
> Thanks a lot
>
> Nicolas
>
>
> Nicolas Degallier
>
> IRD UMR182
> Laboratoire d'Océanographie et du Climat, Expérimentation et Approches Numériques (LOCEAN)
> Tour 45-55, 4e ét., case 100, 4 place Jussieu
> 75252  Paris Cedex 5  France
> tél: (33) 01 44 27 51 57
> fax: (33) 01 44 27 38 05
>
> E-mail: <[hidden email]> <[hidden email]>
> pdf reprints:http://www.locean-ipsl.upmc.fr/~ndelod/production/
>
> Skype:"xuxaxu"
>
> _______________________________________________
> R-sig-Geo mailing [hidden email]://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
>
> _______________________________________________
> R-sig-Geo mailing list
> [hidden email]
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
>
        [[alternative HTML version deleted]]


_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo