Re: [R-sig-Geo] Combine two polygons

2023-04-28 Thread Miluji Sb
Dear Ben,

Thank you for your help. I will try this out. What I want to do is combine
the polygons of Cyprus and Northern Cyprus into one single polygon
eliminating the border. For the GADM, I am only interested in the
country-level shapefile. Thanks again!

Best regards,

Milu

On Fri, Apr 28, 2023 at 4:41 PM Ben Tupper  wrote:

> Hi Milu,
>
> If you want to combine just a few of the thousands of features in
> world then you will have to manage them individually.  Essentially
> extract the subset to be combined, do the combination steps, and then
> add them back to the world.  I am very not familiar with GADM and I
> only have version 4.1, but all of the attributes appear to be
> character type, which raises the question about what you want
> aggregate() to do - it can't take the mean of a character attribute.
> In the example below I show how you might take just the first value of
> each attribute.  While this combining is possible, it doesn't mean
> that you will have "correct" attributes with your new single feature.
> I guess you'll have to figure out what you really want to do with
> that.
>
> ### start here
> library(sf)
> library(dplyr)
> # a function called by aggregate() for each attribute
> first_item = function(x){
>   x[[1]]
> }
> # read in the database downlaoded from https://gadm.org/data.html
> world = read_sf("~/Downloads/gadm_410.gpkg")
> # find the Cypriot features (we'll use this twice)
> ix = grepl("Cyp", world$NAME_0, fixed = TRUE)
> # pull them out of world
> Cyp_features = dplyr::filter(world, ix)
> # aggregate - which works for the geometry but is wrong in terms of
> handling the attributes
> Cyp_union = aggregate(Cyp_features,
>   sf::st_union(Cyp_features),
>   first_item)
> # bind the world (less Cypriot features) with the combined Cypriot
> aggregation
> new_world = dplyr::bind_rows(dplyr::filter(world, !ix), Cyp_union)
> ### end here
>
>
> Does that help?
>
> Ben
>
>
> On Fri, Apr 28, 2023 at 8:28 AM Roger Bivand  wrote:
> >
> > Briefly, is sf::aggregate() what you are looking for? Or
> terra::aggregate() if you read with terra::vect()?
> >
> > ---
> > Roger Bivand
> > Emeritus Professor
> > Department of Economics
> > Norwegian School of Economics, Bergen, Norway
> >
> > ________
> > Fra: Miluji Sb 
> > Sendt: fredag 28. april 2023, 14:18
> > Til: Roger Bivand 
> > Kopi: Michael Sumner ; R-sig-geo mailing list <
> r-sig-geo@r-project.org>
> > Emne: Re: [R-sig-Geo] Combine two polygons
> >
> > Thank you for your replies. I think I am misunderstanding something. I
> have read the shapefile using sf. I would like to combine polygons for
> Cyprus and Northern Cyprus only within the world shapefile and keep all the
> other polygons as they are.
> >
> > world <- sf::st_read(dsn = "~/gadm36_levels_shp", layer = "gadm36_0")
> >
> > Is something like the following correct? I was thinking of generating a
> group variable which is unique to all the countries except Cyprus and
> Northern Cyprus. I a probably making this too complicated.
> >
> > world %>%
> >   group_by(group) %>%
> >   summarise(geometry = sf::st_union(geometry)) %>%
> >   ungroup()
> >
> > Thank you again.
> >
> > Best regards,
> >
> > Milu
> >
> > On Fri, Apr 28, 2023 at 8:56 AM Roger Bivand  <mailto:roger.biv...@nhh.no>> wrote:
> > On Fri, 28 Apr 2023, Michael Sumner wrote:
> >
> > > fwiw, a method with geodata/terra
> > >
> > > cyp <- geodata::gadm(country = c("CYP", "XNC"), path = tempdir(),
> version =
> > > "3.6")
> > >
> > > terra::aggregate(cyp)
> > >
> >
> > and as per
> > https://github.com/r-spatial/evolution/blob/main/pkgapi_230305_refs.csv.
> >
> > > see ?terra::writeVector to write it out
> > >
> > > (rgdal is very old and you should abandon it, it will be removed from
> > > support by the end of this year)
> >
> > Thank you! Yes, sp will by default switch to using sf in place of rgdal
> > for creating "CRS" objects, and under sp::spTransform in June, and all
> > three retiring packages rgdal, rgeos, and maptools will be gone by
> October
> > 2023. An sp release will appear in May with a transition guide.
> >
> > It seems that terra does not have an object corresponding to sp's "CRS"
> or
> > sf's "crs". As far as I can see, one needs to create an empty
>

Re: [R-sig-Geo] Combine two polygons

2023-04-28 Thread Ben Tupper
Hi Milu,

If you want to combine just a few of the thousands of features in
world then you will have to manage them individually.  Essentially
extract the subset to be combined, do the combination steps, and then
add them back to the world.  I am very not familiar with GADM and I
only have version 4.1, but all of the attributes appear to be
character type, which raises the question about what you want
aggregate() to do - it can't take the mean of a character attribute.
In the example below I show how you might take just the first value of
each attribute.  While this combining is possible, it doesn't mean
that you will have "correct" attributes with your new single feature.
I guess you'll have to figure out what you really want to do with
that.

### start here
library(sf)
library(dplyr)
# a function called by aggregate() for each attribute
first_item = function(x){
  x[[1]]
}
# read in the database downlaoded from https://gadm.org/data.html
world = read_sf("~/Downloads/gadm_410.gpkg")
# find the Cypriot features (we'll use this twice)
ix = grepl("Cyp", world$NAME_0, fixed = TRUE)
# pull them out of world
Cyp_features = dplyr::filter(world, ix)
# aggregate - which works for the geometry but is wrong in terms of
handling the attributes
Cyp_union = aggregate(Cyp_features,
  sf::st_union(Cyp_features),
  first_item)
# bind the world (less Cypriot features) with the combined Cypriot aggregation
new_world = dplyr::bind_rows(dplyr::filter(world, !ix), Cyp_union)
### end here


Does that help?

Ben


On Fri, Apr 28, 2023 at 8:28 AM Roger Bivand  wrote:
>
> Briefly, is sf::aggregate() what you are looking for? Or terra::aggregate() 
> if you read with terra::vect()?
>
> ---
> Roger Bivand
> Emeritus Professor
> Department of Economics
> Norwegian School of Economics, Bergen, Norway
>
> 
> Fra: Miluji Sb 
> Sendt: fredag 28. april 2023, 14:18
> Til: Roger Bivand 
> Kopi: Michael Sumner ; R-sig-geo mailing list 
> 
> Emne: Re: [R-sig-Geo] Combine two polygons
>
> Thank you for your replies. I think I am misunderstanding something. I have 
> read the shapefile using sf. I would like to combine polygons for Cyprus and 
> Northern Cyprus only within the world shapefile and keep all the other 
> polygons as they are.
>
> world <- sf::st_read(dsn = "~/gadm36_levels_shp", layer = "gadm36_0")
>
> Is something like the following correct? I was thinking of generating a group 
> variable which is unique to all the countries except Cyprus and Northern 
> Cyprus. I a probably making this too complicated.
>
> world %>%
>   group_by(group) %>%
>   summarise(geometry = sf::st_union(geometry)) %>%
>   ungroup()
>
> Thank you again.
>
> Best regards,
>
> Milu
>
> On Fri, Apr 28, 2023 at 8:56 AM Roger Bivand 
> mailto:roger.biv...@nhh.no>> wrote:
> On Fri, 28 Apr 2023, Michael Sumner wrote:
>
> > fwiw, a method with geodata/terra
> >
> > cyp <- geodata::gadm(country = c("CYP", "XNC"), path = tempdir(), version =
> > "3.6")
> >
> > terra::aggregate(cyp)
> >
>
> and as per
> https://github.com/r-spatial/evolution/blob/main/pkgapi_230305_refs.csv.
>
> > see ?terra::writeVector to write it out
> >
> > (rgdal is very old and you should abandon it, it will be removed from
> > support by the end of this year)
>
> Thank you! Yes, sp will by default switch to using sf in place of rgdal
> for creating "CRS" objects, and under sp::spTransform in June, and all
> three retiring packages rgdal, rgeos, and maptools will be gone by October
> 2023. An sp release will appear in May with a transition guide.
>
> It seems that terra does not have an object corresponding to sp's "CRS" or
> sf's "crs". As far as I can see, one needs to create an empty
> SpatVector/SpatRaster object and assign a CRS, coerce to "Spatial", and
> extract its slot. If this makes sense, sp could use terra (and
> raster, needed for coercion) rather than sf where workflows did not
> otherwise use sf. Very grateful for input here or
> https://github.com/r-spatial/evolution/issues.
>
> Roger
>
> >
> >
> > (just as an aside, we can't hit the geodata vector urls directly with gdal
> > as we can with the rasters, as they are in an R specific format)
> >
> > Cheers, Mike
> >
> >
> > On Fri, Apr 28, 2023 at 10:52 AM Ben Tupper 
> > mailto:btup...@bigelow.org>> wrote:
> >
> >> Hi,
> >>
> >> You might try st_union() from the sf package
> >> https://r-spatial.github.io/sf/articles/sf3.html#geometrical-operat

Re: [R-sig-Geo] Combine two polygons

2023-04-28 Thread Roger Bivand
Briefly, is sf::aggregate() what you are looking for? Or terra::aggregate() if 
you read with terra::vect()?

---
Roger Bivand
Emeritus Professor
Department of Economics
Norwegian School of Economics, Bergen, Norway


Fra: Miluji Sb 
Sendt: fredag 28. april 2023, 14:18
Til: Roger Bivand 
Kopi: Michael Sumner ; R-sig-geo mailing list 

Emne: Re: [R-sig-Geo] Combine two polygons

Thank you for your replies. I think I am misunderstanding something. I have 
read the shapefile using sf. I would like to combine polygons for Cyprus and 
Northern Cyprus only within the world shapefile and keep all the other polygons 
as they are.

world <- sf::st_read(dsn = "~/gadm36_levels_shp", layer = "gadm36_0")

Is something like the following correct? I was thinking of generating a group 
variable which is unique to all the countries except Cyprus and Northern 
Cyprus. I a probably making this too complicated.

world %>%
  group_by(group) %>%
  summarise(geometry = sf::st_union(geometry)) %>%
  ungroup()

Thank you again.

Best regards,

Milu

On Fri, Apr 28, 2023 at 8:56 AM Roger Bivand 
mailto:roger.biv...@nhh.no>> wrote:
On Fri, 28 Apr 2023, Michael Sumner wrote:

> fwiw, a method with geodata/terra
>
> cyp <- geodata::gadm(country = c("CYP", "XNC"), path = tempdir(), version =
> "3.6")
>
> terra::aggregate(cyp)
>

and as per
https://github.com/r-spatial/evolution/blob/main/pkgapi_230305_refs.csv.

> see ?terra::writeVector to write it out
>
> (rgdal is very old and you should abandon it, it will be removed from
> support by the end of this year)

Thank you! Yes, sp will by default switch to using sf in place of rgdal
for creating "CRS" objects, and under sp::spTransform in June, and all
three retiring packages rgdal, rgeos, and maptools will be gone by October
2023. An sp release will appear in May with a transition guide.

It seems that terra does not have an object corresponding to sp's "CRS" or
sf's "crs". As far as I can see, one needs to create an empty
SpatVector/SpatRaster object and assign a CRS, coerce to "Spatial", and
extract its slot. If this makes sense, sp could use terra (and
raster, needed for coercion) rather than sf where workflows did not
otherwise use sf. Very grateful for input here or
https://github.com/r-spatial/evolution/issues.

Roger

>
>
> (just as an aside, we can't hit the geodata vector urls directly with gdal
> as we can with the rasters, as they are in an R specific format)
>
> Cheers, Mike
>
>
> On Fri, Apr 28, 2023 at 10:52 AM Ben Tupper 
> mailto:btup...@bigelow.org>> wrote:
>
>> Hi,
>>
>> You might try st_union() from the sf package
>> https://r-spatial.github.io/sf/articles/sf3.html#geometrical-operations
>>
>>
>>
>> On Thu, Apr 27, 2023 at 5:37 PM Miluji Sb 
>> mailto:miluj...@gmail.com>> wrote:
>>
>>> Dear all,
>>>
>>> I am using the country-level shapefile from GADM. I would like to merge
>>> Cyprus and North Cyprus into one polygon. Is this possible? I am using
>>> rgdal to read the shapefile, subset, and then rewrite it. Any help will
>> be
>>> highly appreciated.
>>>
>>> ##
>>> world <- readOGR("~/gadm36_levels_shp", layer = "gadm36_0")
>>>
>>> Best regards,
>>>
>>> Milu
>>>
>>> [[alternative HTML version deleted]]
>>>
>>> ___
>>> R-sig-Geo mailing list
>>> R-sig-Geo@r-project.org<mailto:R-sig-Geo@r-project.org>
>>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>>
>>
>> [[alternative HTML version deleted]]
>>
>> ___
>> R-sig-Geo mailing list
>> R-sig-Geo@r-project.org<mailto:R-sig-Geo@r-project.org>
>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>
>
>
>

--
Roger Bivand
Emeritus Professor
Department of Economics, Norwegian School of Economics,
Postboks 3490 Ytre Sandviken, 5045 Bergen, Norway.
e-mail: roger.biv...@nhh.no<mailto:roger.biv...@nhh.no>
https://orcid.org/-0003-2392-6140
https://scholar.google.no/citations?user=AWeghB0J=en___
R-sig-Geo mailing list
R-sig-Geo@r-project.org<mailto:R-sig-Geo@r-project.org>
https://stat.ethz.ch/mailman/listinfo/r-sig-geo


[[alternative HTML version deleted]]

___
R-sig-Geo mailing list
R-sig-Geo@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-geo


Re: [R-sig-Geo] Combine two polygons

2023-04-28 Thread Miluji Sb
Thank you for your replies. I think I am misunderstanding something. I have
read the shapefile using sf. I would like to combine polygons for Cyprus
and Northern Cyprus only within the world shapefile and keep all the other
polygons as they are.

world <- sf::st_read(dsn = "~/gadm36_levels_shp", layer = "gadm36_0")

Is something like the following correct? I was thinking of generating a
group variable which is unique to all the countries except Cyprus and
Northern Cyprus. I a probably making this too complicated.

world %>%
  group_by(group) %>%
  summarise(geometry = sf::st_union(geometry)) %>%
  ungroup()

Thank you again.

Best regards,

Milu

On Fri, Apr 28, 2023 at 8:56 AM Roger Bivand  wrote:

> On Fri, 28 Apr 2023, Michael Sumner wrote:
>
> > fwiw, a method with geodata/terra
> >
> > cyp <- geodata::gadm(country = c("CYP", "XNC"), path = tempdir(),
> version =
> > "3.6")
> >
> > terra::aggregate(cyp)
> >
>
> and as per
> https://github.com/r-spatial/evolution/blob/main/pkgapi_230305_refs.csv.
>
> > see ?terra::writeVector to write it out
> >
> > (rgdal is very old and you should abandon it, it will be removed from
> > support by the end of this year)
>
> Thank you! Yes, sp will by default switch to using sf in place of rgdal
> for creating "CRS" objects, and under sp::spTransform in June, and all
> three retiring packages rgdal, rgeos, and maptools will be gone by October
> 2023. An sp release will appear in May with a transition guide.
>
> It seems that terra does not have an object corresponding to sp's "CRS" or
> sf's "crs". As far as I can see, one needs to create an empty
> SpatVector/SpatRaster object and assign a CRS, coerce to "Spatial", and
> extract its slot. If this makes sense, sp could use terra (and
> raster, needed for coercion) rather than sf where workflows did not
> otherwise use sf. Very grateful for input here or
> https://github.com/r-spatial/evolution/issues.
>
> Roger
>
> >
> >
> > (just as an aside, we can't hit the geodata vector urls directly with
> gdal
> > as we can with the rasters, as they are in an R specific format)
> >
> > Cheers, Mike
> >
> >
> > On Fri, Apr 28, 2023 at 10:52 AM Ben Tupper  wrote:
> >
> >> Hi,
> >>
> >> You might try st_union() from the sf package
> >> https://r-spatial.github.io/sf/articles/sf3.html#geometrical-operations
> >>
> >>
> >>
> >> On Thu, Apr 27, 2023 at 5:37 PM Miluji Sb  wrote:
> >>
> >>> Dear all,
> >>>
> >>> I am using the country-level shapefile from GADM. I would like to merge
> >>> Cyprus and North Cyprus into one polygon. Is this possible? I am using
> >>> rgdal to read the shapefile, subset, and then rewrite it. Any help will
> >> be
> >>> highly appreciated.
> >>>
> >>> ##
> >>> world <- readOGR("~/gadm36_levels_shp", layer = "gadm36_0")
> >>>
> >>> Best regards,
> >>>
> >>> Milu
> >>>
> >>> [[alternative HTML version deleted]]
> >>>
> >>> ___
> >>> R-sig-Geo mailing list
> >>> R-sig-Geo@r-project.org
> >>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
> >>>
> >>
> >> [[alternative HTML version deleted]]
> >>
> >> ___
> >> R-sig-Geo mailing list
> >> R-sig-Geo@r-project.org
> >> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
> >>
> >
> >
> >
>
> --
> Roger Bivand
> Emeritus Professor
> Department of Economics, Norwegian School of Economics,
> Postboks 3490 Ytre Sandviken, 5045 Bergen, Norway.
> e-mail: roger.biv...@nhh.no
> https://orcid.org/-0003-2392-6140
> https://scholar.google.no/citations?user=AWeghB0J=en
> ___
> R-sig-Geo mailing list
> R-sig-Geo@r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

[[alternative HTML version deleted]]

___
R-sig-Geo mailing list
R-sig-Geo@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-geo


Re: [R-sig-Geo] Combine two polygons

2023-04-28 Thread Roger Bivand

On Fri, 28 Apr 2023, Michael Sumner wrote:


fwiw, a method with geodata/terra

cyp <- geodata::gadm(country = c("CYP", "XNC"), path = tempdir(), version =
"3.6")

terra::aggregate(cyp)



and as per 
https://github.com/r-spatial/evolution/blob/main/pkgapi_230305_refs.csv.



see ?terra::writeVector to write it out

(rgdal is very old and you should abandon it, it will be removed from
support by the end of this year)


Thank you! Yes, sp will by default switch to using sf in place of rgdal 
for creating "CRS" objects, and under sp::spTransform in June, and all 
three retiring packages rgdal, rgeos, and maptools will be gone by October 
2023. An sp release will appear in May with a transition guide.


It seems that terra does not have an object corresponding to sp's "CRS" or 
sf's "crs". As far as I can see, one needs to create an empty 
SpatVector/SpatRaster object and assign a CRS, coerce to "Spatial", and 
extract its slot. If this makes sense, sp could use terra (and 
raster, needed for coercion) rather than sf where workflows did not 
otherwise use sf. Very grateful for input here or 
https://github.com/r-spatial/evolution/issues.


Roger




(just as an aside, we can't hit the geodata vector urls directly with gdal
as we can with the rasters, as they are in an R specific format)

Cheers, Mike


On Fri, Apr 28, 2023 at 10:52 AM Ben Tupper  wrote:


Hi,

You might try st_union() from the sf package
https://r-spatial.github.io/sf/articles/sf3.html#geometrical-operations



On Thu, Apr 27, 2023 at 5:37 PM Miluji Sb  wrote:


Dear all,

I am using the country-level shapefile from GADM. I would like to merge
Cyprus and North Cyprus into one polygon. Is this possible? I am using
rgdal to read the shapefile, subset, and then rewrite it. Any help will

be

highly appreciated.

##
world <- readOGR("~/gadm36_levels_shp", layer = "gadm36_0")

Best regards,

Milu

[[alternative HTML version deleted]]

___
R-sig-Geo mailing list
R-sig-Geo@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-geo



[[alternative HTML version deleted]]

___
R-sig-Geo mailing list
R-sig-Geo@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-geo







--
Roger Bivand
Emeritus Professor
Department of Economics, Norwegian School of Economics,
Postboks 3490 Ytre Sandviken, 5045 Bergen, Norway.
e-mail: roger.biv...@nhh.no
https://orcid.org/-0003-2392-6140
https://scholar.google.no/citations?user=AWeghB0J=en___
R-sig-Geo mailing list
R-sig-Geo@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-geo


Re: [R-sig-Geo] Combine two polygons

2023-04-27 Thread Michael Sumner
fwiw, a method with geodata/terra

cyp <- geodata::gadm(country = c("CYP", "XNC"), path = tempdir(), version =
"3.6")

terra::aggregate(cyp)

see ?terra::writeVector to write it out

(rgdal is very old and you should abandon it, it will be removed from
support by the end of this year)


(just as an aside, we can't hit the geodata vector urls directly with gdal
as we can with the rasters, as they are in an R specific format)

Cheers, Mike


On Fri, Apr 28, 2023 at 10:52 AM Ben Tupper  wrote:

> Hi,
>
> You might try st_union() from the sf package
> https://r-spatial.github.io/sf/articles/sf3.html#geometrical-operations
>
>
>
> On Thu, Apr 27, 2023 at 5:37 PM Miluji Sb  wrote:
>
> > Dear all,
> >
> > I am using the country-level shapefile from GADM. I would like to merge
> > Cyprus and North Cyprus into one polygon. Is this possible? I am using
> > rgdal to read the shapefile, subset, and then rewrite it. Any help will
> be
> > highly appreciated.
> >
> > ##
> > world <- readOGR("~/gadm36_levels_shp", layer = "gadm36_0")
> >
> > Best regards,
> >
> > Milu
> >
> > [[alternative HTML version deleted]]
> >
> > ___
> > R-sig-Geo mailing list
> > R-sig-Geo@r-project.org
> > https://stat.ethz.ch/mailman/listinfo/r-sig-geo
> >
>
> [[alternative HTML version deleted]]
>
> ___
> R-sig-Geo mailing list
> R-sig-Geo@r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>


-- 
Michael Sumner
Software and Database Engineer
Australian Antarctic Division
Hobart, Australia
e-mail: mdsum...@gmail.com

[[alternative HTML version deleted]]

___
R-sig-Geo mailing list
R-sig-Geo@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-geo


Re: [R-sig-Geo] Combine two polygons

2023-04-27 Thread Ben Tupper
Hi,

You might try st_union() from the sf package
https://r-spatial.github.io/sf/articles/sf3.html#geometrical-operations



On Thu, Apr 27, 2023 at 5:37 PM Miluji Sb  wrote:

> Dear all,
>
> I am using the country-level shapefile from GADM. I would like to merge
> Cyprus and North Cyprus into one polygon. Is this possible? I am using
> rgdal to read the shapefile, subset, and then rewrite it. Any help will be
> highly appreciated.
>
> ##
> world <- readOGR("~/gadm36_levels_shp", layer = "gadm36_0")
>
> Best regards,
>
> Milu
>
> [[alternative HTML version deleted]]
>
> ___
> R-sig-Geo mailing list
> R-sig-Geo@r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

[[alternative HTML version deleted]]

___
R-sig-Geo mailing list
R-sig-Geo@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-geo