Re: [R-sig-Geo] How to create inward (shrinking) buffer zones with st_buffer()

2024-02-25 Thread Xiang Ye via R-sig-Geo
Dear Nathaniel,

Thank you for suggesting a new (and more advanced) way to tackle this issue. 
Your solution is obviously with better flexibility. In particular I appreciate 
your explicit adoption of units::set_units() to remove ambiguity.
Thank you for the help!

叶翔 YE, Xiang
THINKING SPATIALLY<http://www.linkedin.com/in/spatialyexiang>.
Ph.D. in Spatial Statistics

From: Nathaniel Henry 
Sent: Friday, February 23, 2024 3:46
To: Xiang Ye 
Cc: [email protected] 
Subject: Re: [R-sig-Geo] How to create inward (shrinking) buffer zones with 
st_buffer()

You don't often get email from [email protected]. Learn why this 
is important<https://aka.ms/LearnAboutSenderIdentification>
Hi Xiang,

As an alternative, you could just convert the polygon boundaries to lines, 
buffer those lines, and remove them from the original polygons. I think this 
will work regardless of your CRS/s2 settings, and you don't have to worry about 
how the buffer function is handling negative distance internally.

Example:
bristol_polys <- sf::st_read(
  
"https://martinjc.github.io/UK-GeoJSON/json/eng/wards_by_lad/topo_E0623.json";,
  crs = 'EPSG:4326'
) |> sf::st_transform(crs = 'EPSG:27700')
# Convert to polylines and buffer
buffered_lines <- sf::st_cast(bristol_polys, to = 'MULTILINESTRING') |>
  sf::st_make_valid() |>
  sf::st_buffer(dist = units::set_units(100, 'm')) |>
  sf::st_union()
# Remove the buffers from the polygons
shrunk_polygons <- sf::st_difference(x = bristol_polys, y = buffered_lines)

Yields:
[image.png]

Best,
Nat
--
Nathaniel Henry, DPhil
Henry Spatial Analysis<https://henryspatialanalysis.com/>
Cell: +1-341-226-6277

On Thu, 22 Feb 2024 at 08:28, Nick Bearman 
mailto:[email protected]>>
 wrote:
Dear Xiang,

I've not had a chance to try your code, but your example probably won't
work with the CRS set to WGS84. The buffer will use the units of the
coordinate system, so degrees, which will be far too large for the
bristol_zones example.

Assuming bristol_zones are in the UK, you need to reproject to BNG - 27700.

Also I am not sure whether bristol_zones from spDataLarge are in sp or
sf format.

Once those are addressed - I see no reason why the example in the Stack
Exchange should work.

Best wishes,
Nick.

On 19/02/2024 10:04, Xiang Ye via R-sig-Geo wrote:
> Dear community,
>
> I am learning some basic geometry operation functions of sf package including 
> st_buffer().
>
> It seems there should be no wonder if I provide a negative value to the dist 
> argument in st_buffer(), I should expect an inward/shrinking buffer zone (I 
> also followed here: 
> https://gis.stackexchange.com/questions/392505/can-i-use-r-to-do-a-buffer-inside-polygons-shrink-polygons-negative-buffer).
>  However, it turn out to be as long as I provide a negative value, the output 
> will be an empty geometry:
>
> library(sf)
> library(spDataLarge)
> st_geometry(bristol_zones[1, ]) -> a# a is the exemplary data set
>
>> a
> Geometry set for 1 feature
> Geometry type: MULTIPOLYGON
> Dimension: XY
> Bounding box:  xmin: -2.534502 ymin: 51.40487 xmax: -2.488435 ymax: 51.43478
> Geodetic CRS:  WGS 84
> MULTIPOLYGON (((-2.510462 51.42878, -2.507985 5...
>> st_buffer(a, 100)
> Geometry set for 1 feature
> Geometry type: POLYGON
> Dimension: XY
> Bounding box:  xmin: -2.536248 ymin: 51.40393 xmax: -2.486907 ymax: 51.43598
> Geodetic CRS:  WGS 84
> POLYGON ((-2.517834 51.43188, -2.518218 51.4318...
>> st_buffer(a, -100)
> Geometry set for 1 feature  (with 1 geometry empty)
> Geometry type: POLYGON
> Dimension: XY
> Bounding box:  xmin: NA ymin: NA xmax: NA ymax: NA
> Geodetic CRS:  WGS 84
> POLYGON EMPTY
>
> So I would like to know if it is possible to create inward buffer zones with 
> st_buffer()? If st_buffer() is not designed to perform this, what is the best 
> alternative?
>
> Thank you, and have a great start of the week!
>
> 叶翔 YE, Xiang
> THINKING SPATIALLY<http://www.linkedin.com/in/spatialyexiang>.
> Ph.D. in Spatial Statistics
>
>   [[alternative HTML version deleted]]
>
> ___
> R-sig-Geo mailing list
> [email protected]<mailto:[email protected]>
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
--
Nick Bearman
+44 (0) 7717745715
[email protected]<mailto:[email protected]>

Please let me know if I can make any adjustments related to disability or 
neurodivergence to improve how we interact.

Due to my own life/work balance, you may get emails from me outside of normal 
working hours. Please do not feel any pressure to respond outs

Re: [R-sig-Geo] How to create inward (shrinking) buffer zones with st_buffer()

2024-02-25 Thread Xiang Ye via R-sig-Geo
Dear Nick,

Thank you for your reply! And I agree with several points mentioned in your 
email, as the geographic coordinate system is not the best CRS to generate a 
buffer zone. According to the guide from previous replies to my question, I 
turned off the Google s2 geometry library by setting sf_use_s2(FALSE) then 
st_buffer() will interpret the dist argument in degrees.

Btw, as suggested by class(bristol_zones), it is an sf object.

Thank you and have a great start of the week!



$B3pfF(B YE, Xiang
THINKING SPATIALLY<http://www.linkedin.com/in/spatialyexiang>.
Ph.D. in Spatial Statistics

From: R-sig-Geo  on behalf of Nick Bearman 

Sent: Friday, February 23, 2024 0:28
To: [email protected] 
Subject: Re: [R-sig-Geo] How to create inward (shrinking) buffer zones with 
st_buffer()

[You don't often get email from [email protected]. Learn 
why this is important at https://aka.ms/LearnAboutSenderIdentification ]

Dear Xiang,

I've not had a chance to try your code, but your example probably won't
work with the CRS set to WGS84. The buffer will use the units of the
coordinate system, so degrees, which will be far too large for the
bristol_zones example.

Assuming bristol_zones are in the UK, you need to reproject to BNG - 27700.

Also I am not sure whether bristol_zones from spDataLarge are in sp or
sf format.

Once those are addressed - I see no reason why the example in the Stack
Exchange should work.

Best wishes,
Nick.

On 19/02/2024 10:04, Xiang Ye via R-sig-Geo wrote:
> Dear community,
>
> I am learning some basic geometry operation functions of sf package including 
> st_buffer().
>
> It seems there should be no wonder if I provide a negative value to the dist 
> argument in st_buffer(), I should expect an inward/shrinking buffer zone (I 
> also followed here: 
> https://nam12.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgis.stackexchange.com%2Fquestions%2F392505%2Fcan-i-use-r-to-do-a-buffer-inside-polygons-shrink-polygons-negative-buffer&data=05%7C02%7Cxiangye%40buffalo.edu%7Cdfd6d699011c443c102808dc33c3555d%7C96464a8af8ed40b199e25f6b50a20250%7C0%7C0%7C638442161268034782%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C0%7C%7C%7C&sdata=qb0R4h1gmWzIeZOaFJkjtdpylR6WprDzQcFGiJJ5rDs%3D&reserved=0)<https://gis.stackexchange.com/questions/392505/can-i-use-r-to-do-a-buffer-inside-polygons-shrink-polygons-negative-buffer>.
>  However, it turn out to be as long as I provide a negative value, the output 
> will be an empty geometry:
>
> library(sf)
> library(spDataLarge)
> st_geometry(bristol_zones[1, ]) -> a# a is the exemplary data set
>
>> a
> Geometry set for 1 feature
> Geometry type: MULTIPOLYGON
> Dimension: XY
> Bounding box:  xmin: -2.534502 ymin: 51.40487 xmax: -2.488435 ymax: 51.43478
> Geodetic CRS:  WGS 84
> MULTIPOLYGON (((-2.510462 51.42878, -2.507985 5...
>> st_buffer(a, 100)
> Geometry set for 1 feature
> Geometry type: POLYGON
> Dimension: XY
> Bounding box:  xmin: -2.536248 ymin: 51.40393 xmax: -2.486907 ymax: 51.43598
> Geodetic CRS:  WGS 84
> POLYGON ((-2.517834 51.43188, -2.518218 51.4318...
>> st_buffer(a, -100)
> Geometry set for 1 feature  (with 1 geometry empty)
> Geometry type: POLYGON
> Dimension: XY
> Bounding box:  xmin: NA ymin: NA xmax: NA ymax: NA
> Geodetic CRS:  WGS 84
> POLYGON EMPTY
>
> So I would like to know if it is possible to create inward buffer zones with 
> st_buffer()? If st_buffer() is not designed to perform this, what is the best 
> alternative?
>
> Thank you, and have a great start of the week!
>
> $B3pfF(B YE, Xiang
> THINKING 
> SPATIALLY<https://nam12.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.linkedin.com%2Fin%2Fspatialyexiang&data=05%7C02%7Cxiangye%40buffalo.edu%7Cdfd6d699011c443c102808dc33c3555d%7C96464a8af8ed40b199e25f6b50a20250%7C0%7C0%7C638442161268045734%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C0%7C%7C%7C&sdata=fim9H15Z3SZAoak1FEbs63Ovgosr5nZVwY4MCo0npB4%3D&reserved=0<http://www.linkedin.com/in/spatialyexiang>>.
> Ph.D. in Spatial Statistics
>
>   [[alternative HTML version deleted]]
>
> ___
> R-sig-Geo mailing list
> [email protected]
> https://nam12.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-sig-geo&data=05%7C02%7Cxiangye%40buffalo.edu%7Cdfd6d699011c443c102808dc33c3555d%7C96464a8af8ed40b199e25f6b50a20250%7C0%7C0%7C638442161268051713%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C0%7C%7C%7C&sdata=0EAPpfFBELTd3FWmskvu18QbgNayTdtB%2BbjE4k3WIOw%3D&reserved=0<http

Re: [R-sig-Geo] How to create inward (shrinking) buffer zones with st_buffer()

2024-02-22 Thread Nick Bearman

Dear Xiang,

I've not had a chance to try your code, but your example probably won't 
work with the CRS set to WGS84. The buffer will use the units of the 
coordinate system, so degrees, which will be far too large for the 
bristol_zones example.


Assuming bristol_zones are in the UK, you need to reproject to BNG - 27700.

Also I am not sure whether bristol_zones from spDataLarge are in sp or 
sf format.


Once those are addressed - I see no reason why the example in the Stack 
Exchange should work.


Best wishes,
Nick.

On 19/02/2024 10:04, Xiang Ye via R-sig-Geo wrote:

Dear community,

I am learning some basic geometry operation functions of sf package including 
st_buffer().

It seems there should be no wonder if I provide a negative value to the dist 
argument in st_buffer(), I should expect an inward/shrinking buffer zone (I 
also followed here: 
https://gis.stackexchange.com/questions/392505/can-i-use-r-to-do-a-buffer-inside-polygons-shrink-polygons-negative-buffer).
 However, it turn out to be as long as I provide a negative value, the output 
will be an empty geometry:

library(sf)
library(spDataLarge)
st_geometry(bristol_zones[1, ]) -> a# a is the exemplary data set


a

Geometry set for 1 feature
Geometry type: MULTIPOLYGON
Dimension: XY
Bounding box:  xmin: -2.534502 ymin: 51.40487 xmax: -2.488435 ymax: 51.43478
Geodetic CRS:  WGS 84
MULTIPOLYGON (((-2.510462 51.42878, -2.507985 5...

st_buffer(a, 100)

Geometry set for 1 feature
Geometry type: POLYGON
Dimension: XY
Bounding box:  xmin: -2.536248 ymin: 51.40393 xmax: -2.486907 ymax: 51.43598
Geodetic CRS:  WGS 84
POLYGON ((-2.517834 51.43188, -2.518218 51.4318...

st_buffer(a, -100)

Geometry set for 1 feature  (with 1 geometry empty)
Geometry type: POLYGON
Dimension: XY
Bounding box:  xmin: NA ymin: NA xmax: NA ymax: NA
Geodetic CRS:  WGS 84
POLYGON EMPTY

So I would like to know if it is possible to create inward buffer zones with 
st_buffer()? If st_buffer() is not designed to perform this, what is the best 
alternative?

Thank you, and have a great start of the week!

$B3pfF(B YE, Xiang
THINKING SPATIALLY.
Ph.D. in Spatial Statistics

[[alternative HTML version deleted]]

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


--
Nick Bearman
+44 (0) 7717745715
[email protected]

Please let me know if I can make any adjustments related to disability or 
neurodivergence to improve how we interact.

Due to my own life/work balance, you may get emails from me outside of normal 
working hours. Please do not feel any pressure to respond outside of your own 
working pattern.

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


Re: [R-sig-Geo] How to create inward (shrinking) buffer zones with st_buffer()

2024-02-19 Thread Xiang Ye via R-sig-Geo
Dear Ákos,

Thank you so much for your quick and detailed reply!
I did not know about Google s2geometry.io library before, but now I noticed its 
impact on the sf package.

Have a great one!

叶翔 YE, Xiang
THINKING SPATIALLY<http://www.linkedin.com/in/spatialyexiang>.
Ph.D. in Spatial Statistics

From: R-sig-Geo  on behalf of Bede-Fazekas 
Ákos 
Sent: Monday, February 19, 2024 18:24
To: [email protected] 
Subject: Re: [R-sig-Geo] How to create inward (shrinking) buffer zones with 
st_buffer()

[You don't often get email from [email protected]. Learn why this is 
important at https://aka.ms/LearnAboutSenderIdentification ]

Dear Xiang,

sure it is possible. I suggest using a CRS that is projected, e.g. 3857
(Web Mercator), or turn off s2 geometry. If you turn off s2, then
st_buffer() will interpret the dist parameter in degrees. Anyway, if you
choose a small enough number, then you'll get a non-empty geometry.

 > sf_use_s2(FALSE)
Spherical geometry (s2) switched off
 > st_buffer(a, -100)
dist is assumed to be in decimal degrees (arc_degrees).
Geometry set for 1 feature  (with 1 geometry empty)
Geometry type: POLYGON
Dimension: XY
Bounding box:  xmin: NA ymin: NA xmax: NA ymax: NA
Geodetic CRS:  WGS 84
POLYGON EMPTY
Warning message:
In st_buffer.sfc(a, -100) :
   st_buffer does not correctly buffer longitude/latitude data
 > st_buffer(a, -0.001)
dist is assumed to be in decimal degrees (arc_degrees).
Geometry set for 1 feature
Geometry type: POLYGON
Dimension: XY
Bounding box:  xmin: -2.533411 ymin: 51.40595 xmax: -2.489763 ymax: 51.4315
Geodetic CRS:  WGS 84
POLYGON ((-2.533083 51.41747, -2.528692 51.4204...
Warning message:
In st_buffer.sfc(a, -0.001) :
   st_buffer does not correctly buffer longitude/latitude data

(I don't know why s2 can't deal with negative distance if the CRS is
WGS-84.)

HTH,
Ákos

Ákos Bede-Fazekas
Centre for Ecological Research, Hungary

2024. 02. 19. 11:04 keltezéssel, Xiang Ye via R-sig-Geo írta:
> Dear community,
>
> I am learning some basic geometry operation functions of sf package including 
> st_buffer().
>
> It seems there should be no wonder if I provide a negative value to the dist 
> argument in st_buffer(), I should expect an inward/shrinking buffer zone (I 
> also followed here: 
> https://nam12.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgis.stackexchange.com%2Fquestions%2F392505%2Fcan-i-use-r-to-do-a-buffer-inside-polygons-shrink-polygons-negative-buffer&data=05%7C02%7Cxiangye%40buffalo.edu%7C3442c930a8b649158f2a08dc3135888f%7C96464a8af8ed40b199e25f6b50a20250%7C0%7C0%7C638439353229473724%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C0%7C%7C%7C&sdata=O%2FqzCFqVDv2%2FmjZWZTjLgYx73eq%2FxYVMz5yQKUW5jWw%3D&reserved=0)<https://gis.stackexchange.com/questions/392505/can-i-use-r-to-do-a-buffer-inside-polygons-shrink-polygons-negative-buffer>.
>  However, it turn out to be as long as I provide a negative value, the output 
> will be an empty geometry:
>
> library(sf)
> library(spDataLarge)
> st_geometry(bristol_zones[1, ]) -> a# a is the exemplary data set
>
>> a
> Geometry set for 1 feature
> Geometry type: MULTIPOLYGON
> Dimension: XY
> Bounding box:  xmin: -2.534502 ymin: 51.40487 xmax: -2.488435 ymax: 51.43478
> Geodetic CRS:  WGS 84
> MULTIPOLYGON (((-2.510462 51.42878, -2.507985 5...
>> st_buffer(a, 100)
> Geometry set for 1 feature
> Geometry type: POLYGON
> Dimension: XY
> Bounding box:  xmin: -2.536248 ymin: 51.40393 xmax: -2.486907 ymax: 51.43598
> Geodetic CRS:  WGS 84
> POLYGON ((-2.517834 51.43188, -2.518218 51.4318...
>> st_buffer(a, -100)
> Geometry set for 1 feature  (with 1 geometry empty)
> Geometry type: POLYGON
> Dimension: XY
> Bounding box:  xmin: NA ymin: NA xmax: NA ymax: NA
> Geodetic CRS:  WGS 84
> POLYGON EMPTY
>
> So I would like to know if it is possible to create inward buffer zones with 
> st_buffer()? If st_buffer() is not designed to perform this, what is the best 
> alternative?
>
> Thank you, and have a great start of the week!
>
> $B3pfF(B YE, Xiang
> THINKING 
> SPATIALLY<https://nam12.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.linkedin.com%2Fin%2Fspatialyexiang&data=05%7C02%7Cxiangye%40buffalo.edu%7C3442c930a8b649158f2a08dc3135888f%7C96464a8af8ed40b199e25f6b50a20250%7C0%7C0%7C638439353229481810%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C0%7C%7C%7C&sdata=LI5uCDlgSC09%2BWqxLiZOTzTQs2dKFZ3wybiWz2Txp8w%3D&reserved=0<http://www.linkedin.com/in/spatialyexiang>>.
> Ph.D. in Spatial Statistics
>
>   [[alternative HTML version deleted]]
>
> ___

Re: [R-sig-Geo] How to create inward (shrinking) buffer zones with st_buffer()

2024-02-19 Thread Bede-Fazekas Ákos

Dear Xiang,

sure it is possible. I suggest using a CRS that is projected, e.g. 3857 
(Web Mercator), or turn off s2 geometry. If you turn off s2, then 
st_buffer() will interpret the dist parameter in degrees. Anyway, if you 
choose a small enough number, then you'll get a non-empty geometry.


> sf_use_s2(FALSE)
Spherical geometry (s2) switched off
> st_buffer(a, -100)
dist is assumed to be in decimal degrees (arc_degrees).
Geometry set for 1 feature  (with 1 geometry empty)
Geometry type: POLYGON
Dimension: XY
Bounding box:  xmin: NA ymin: NA xmax: NA ymax: NA
Geodetic CRS:  WGS 84
POLYGON EMPTY
Warning message:
In st_buffer.sfc(a, -100) :
  st_buffer does not correctly buffer longitude/latitude data
> st_buffer(a, -0.001)
dist is assumed to be in decimal degrees (arc_degrees).
Geometry set for 1 feature
Geometry type: POLYGON
Dimension: XY
Bounding box:  xmin: -2.533411 ymin: 51.40595 xmax: -2.489763 ymax: 51.4315
Geodetic CRS:  WGS 84
POLYGON ((-2.533083 51.41747, -2.528692 51.4204...
Warning message:
In st_buffer.sfc(a, -0.001) :
  st_buffer does not correctly buffer longitude/latitude data

(I don't know why s2 can't deal with negative distance if the CRS is 
WGS-84.)


HTH,
Ákos

Ákos Bede-Fazekas
Centre for Ecological Research, Hungary

2024. 02. 19. 11:04 keltezéssel, Xiang Ye via R-sig-Geo írta:

Dear community,

I am learning some basic geometry operation functions of sf package including 
st_buffer().

It seems there should be no wonder if I provide a negative value to the dist 
argument in st_buffer(), I should expect an inward/shrinking buffer zone (I 
also followed here: 
https://gis.stackexchange.com/questions/392505/can-i-use-r-to-do-a-buffer-inside-polygons-shrink-polygons-negative-buffer).
 However, it turn out to be as long as I provide a negative value, the output 
will be an empty geometry:

library(sf)
library(spDataLarge)
st_geometry(bristol_zones[1, ]) -> a# a is the exemplary data set


a

Geometry set for 1 feature
Geometry type: MULTIPOLYGON
Dimension: XY
Bounding box:  xmin: -2.534502 ymin: 51.40487 xmax: -2.488435 ymax: 51.43478
Geodetic CRS:  WGS 84
MULTIPOLYGON (((-2.510462 51.42878, -2.507985 5...

st_buffer(a, 100)

Geometry set for 1 feature
Geometry type: POLYGON
Dimension: XY
Bounding box:  xmin: -2.536248 ymin: 51.40393 xmax: -2.486907 ymax: 51.43598
Geodetic CRS:  WGS 84
POLYGON ((-2.517834 51.43188, -2.518218 51.4318...

st_buffer(a, -100)

Geometry set for 1 feature  (with 1 geometry empty)
Geometry type: POLYGON
Dimension: XY
Bounding box:  xmin: NA ymin: NA xmax: NA ymax: NA
Geodetic CRS:  WGS 84
POLYGON EMPTY

So I would like to know if it is possible to create inward buffer zones with 
st_buffer()? If st_buffer() is not designed to perform this, what is the best 
alternative?

Thank you, and have a great start of the week!

$B3pfF(B YE, Xiang
THINKING SPATIALLY.
Ph.D. in Spatial Statistics

[[alternative HTML version deleted]]

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



--
Ezt az e-mailt átvizsgálta az Avast AntiVirus szoftver.
www.avast.com

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