<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-2022-jp">
<style type="text/css" style="display:none;"> P {margin-top:0;margin-bottom:0;} </style>
</head>
<body dir="ltr">
<div style="font-family: Verdana, Geneva, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
Dear Nathaniel,</div>
<div style="font-family: Verdana, Geneva, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
<br>
</div>
<div style="font-family: Verdana, Geneva, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
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. </div>
<div style="font-family: Verdana, Geneva, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
Thank you for the help!</div>
<div class="elementToProof">
<div style="font-family: Verdana, Geneva, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div id="Signature">
<div><span style="font-family: Verdana, Geneva, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">叶翔 YE, Xiang</span>
<div style="direction: ltr;"><span style="font-family: Verdana, Geneva, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);"><a href="http://www.linkedin.com/in/spatialyexiang" target="_blank">THINKING SPATIALLY</a>.</span></div>
<div style="direction: ltr;"><span style="font-family: Verdana, Geneva, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">Ph.D. in Spatial Statistics</span></div>
</div>
</div>
</div>
<div id="appendonsend"></div>
<hr style="display:inline-block;width:98%" tabindex="-1">
<div id="divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" style="font-size:11pt" color="#000000"><b>From:</b> Nathaniel Henry <henry.spatial.analysis@gmail.com><br>
<b>Sent:</b> Friday, February 23, 2024 3:46<br>
<b>To:</b> Xiang Ye <xiangye@buffalo.edu><br>
<b>Cc:</b> r-sig-geo@r-project.org <r-sig-geo@r-project.org><br>
<b>Subject:</b> Re: [R-sig-Geo] How to create inward (shrinking) buffer zones with st_buffer()</font>
<div> </div>
</div>
<div>
<table border="0" cellspacing="0" cellpadding="0" width="100%" align="left" style="border:0; display:table; width:100%; table-layout:fixed; border-collapse:seperate; float:none">
<tbody style="display:block">
<tr>
<td valign="middle" width="1px" bgcolor="#A6A6A6" cellpadding="7px 2px 7px 2px" style="padding:7px 2px 7px 2px; background-color:#A6A6A6">
</td>
<td valign="middle" width="100%" bgcolor="#EAEAEA" cellpadding="7px 5px 7px 15px" color="#212121" style="width:100%; background-color:#EAEAEA; padding:7px 5px 7px 15px; font-family:wf_segoe-ui_normal,Segoe UI,Segoe WP,Tahoma,Arial,sans-serif; font-size:12px; font-weight:normal; color:#212121; text-align:left; word-wrap:break-word">
<div>You don't often get email from henry.spatial.analysis@gmail.com. <a href="https://aka.ms/LearnAboutSenderIdentification">
Learn why this is important</a></div>
</td>
<td valign="middle" align="left" width="75px" bgcolor="#EAEAEA" cellpadding="7px 5px 7px 5px" color="#212121" style="width:75px; background-color:#EAEAEA; padding:7px 5px 7px 5px; font-family:wf_segoe-ui_normal,Segoe UI,Segoe WP,Tahoma,Arial,sans-serif; font-size:12px; font-weight:normal; color:#212121; text-align:left; word-wrap:break-word">
</td>
</tr>
</tbody>
</table>
<div>
<div dir="ltr">Hi Xiang,
<div><br>
</div>
<div>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.</div>
<div><br>
</div>
<div>Example:</div>
<div>
<div style="color:rgb(131,148,150); background-color:rgb(0,43,54); font-family:Consolas,"Courier New",monospace; font-size:14px; line-height:19px; white-space:pre">
<div><span style="color:rgb(38,139,210)">bristol_polys</span> <span style="color:rgb(133,153,0)">
<-</span> sf<span style="color:rgb(133,153,0)">::</span>st_read(</div>
<div>  <span style="color:rgb(42,161,152)">"<a href="https://martinjc.github.io/UK-GeoJSON/json/eng/wards_by_lad/topo_E06000023.json" originalsrc="https://martinjc.github.io/UK-GeoJSON/json/eng/wards_by_lad/topo_E06000023.json" shash="mfDScT3SO6U9Cgb83yCEkZWDcq7dMDICGgXg/WI/J2sRtXYec+W2ayDkcgKn9F4oh4MtelKDbR8jsYb15jvJNNCYehpK6vUsQOoFUMvP7lE+4c4tLMXiUQOLvNeeswWX17uEEK//hk8J3G+rCp9BIdu2oxDyvJyi5tSDBGlEgnk=">https://martinjc.github.io/UK-GeoJSON/json/eng/wards_by_lad/topo_E06000023.json</a>"</span>,</div>
<div>  crs <span style="color:rgb(133,153,0)">=</span> <span style="color:rgb(42,161,152)">
'EPSG:4326'</span></div>
<div>) <span style="color:rgb(133,153,0)">|></span> sf<span style="color:rgb(133,153,0)">::</span>st_transform(crs
<span style="color:rgb(133,153,0)">=</span> <span style="color:rgb(42,161,152)">'EPSG:27700'</span>)</div>
<div><span style="color:rgb(88,110,117); font-style:italic"># Convert to polylines and buffer</span></div>
<div><span style="color:rgb(38,139,210)">buffered_lines</span> <span style="color:rgb(133,153,0)">
<-</span> sf<span style="color:rgb(133,153,0)">::</span>st_cast(<span style="color:rgb(38,139,210)">bristol_polys</span>, to
<span style="color:rgb(133,153,0)">=</span> <span style="color:rgb(42,161,152)">'MULTILINESTRING'</span>)
<span style="color:rgb(133,153,0)">|></span></div>
<div>  sf<span style="color:rgb(133,153,0)">::</span>st_make_valid() <span style="color:rgb(133,153,0)">
|></span></div>
<div>  sf<span style="color:rgb(133,153,0)">::</span>st_buffer(dist <span style="color:rgb(133,153,0)">
=</span> units<span style="color:rgb(133,153,0)">::</span>set_units(<span style="color:rgb(211,54,130)">100</span>,
<span style="color:rgb(42,161,152)">'m'</span>)) <span style="color:rgb(133,153,0)">
|></span></div>
<div>  sf<span style="color:rgb(133,153,0)">::</span>st_union()</div>
<div><span style="color:rgb(88,110,117); font-style:italic"># Remove the buffers from the polygons</span></div>
<div><span style="color:rgb(38,139,210)">shrunk_polygons</span> <span style="color:rgb(133,153,0)">
<-</span> sf<span style="color:rgb(133,153,0)">::</span>st_difference(x <span style="color:rgb(133,153,0)">
=</span> <span style="color:rgb(38,139,210)">bristol_polys</span>, y <span style="color:rgb(133,153,0)">
=</span> <span style="color:rgb(38,139,210)">buffered_lines</span>)</div>
</div>
</div>
<div><br>
</div>
<div>Yields:</div>
<div><img alt="image.png" width="265" height="297" style="margin-right:0px" data-outlook-trace="F:1|T:1" src="cid:ii_lsxmsmla0"><br>
</div>
<div><br>
</div>
<div>Best,</div>
<div>Nat</div>
<div>--</div>
<div>
<div dir="ltr" class="x_gmail_signature" data-smartmail="gmail_signature">
<div dir="ltr">
<div style="color:rgb(34,34,34)"><b>Nathaniel Henry, DPhil</b><br>
</div>
<div style="color:rgb(34,34,34)"><a href="https://henryspatialanalysis.com/" originalsrc="https://henryspatialanalysis.com/" shash="j3ewh+56Ym0C/xPAr/9Fb09vqQZ6gynV51fx82w2RDjdYo8eJecB7YOnd43eXClx4la4Mi/W8OSZgQb9ACqDB93HRAOda26TSv04j+7Tf+myJrZSOeAWUyvEM2h8XQh3tfRJAnuiBB85k3rgdMXrMc+hTn7prcKAVS7jgynVmjw=" target="_blank" style="color:rgb(17,85,204)">Henry
 Spatial Analysis</a></div>
<div style="color:rgb(34,34,34)"><i>Cell: +1-341-226-6277</i></div>
</div>
</div>
</div>
</div>
<br>
<div class="x_gmail_quote">
<div dir="ltr" class="x_gmail_attr">On Thu, 22 Feb 2024 at 08:28, Nick Bearman <<a href="mailto:nick@geospatialtrainingsolutions.co.uk">nick@geospatialtrainingsolutions.co.uk</a>> wrote:<br>
</div>
<blockquote class="x_gmail_quote" style="margin:0px 0px 0px 0.8ex; border-left:1px solid rgb(204,204,204); padding-left:1ex">
Dear Xiang,<br>
<br>
I've not had a chance to try your code, but your example probably won't <br>
work with the CRS set to WGS84. The buffer will use the units of the <br>
coordinate system, so degrees, which will be far too large for the <br>
bristol_zones example.<br>
<br>
Assuming bristol_zones are in the UK, you need to reproject to BNG - 27700.<br>
<br>
Also I am not sure whether bristol_zones from spDataLarge are in sp or <br>
sf format.<br>
<br>
Once those are addressed - I see no reason why the example in the Stack <br>
Exchange should work.<br>
<br>
Best wishes,<br>
Nick.<br>
<br>
On 19/02/2024 10:04, Xiang Ye via R-sig-Geo wrote:<br>
> Dear community,<br>
><br>
> I am learning some basic geometry operation functions of sf package including st_buffer().<br>
><br>
> 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:
<a href="https://gis.stackexchange.com/questions/392505/can-i-use-r-to-do-a-buffer-inside-polygons-shrink-polygons-negative-buffer" originalsrc="https://gis.stackexchange.com/questions/392505/can-i-use-r-to-do-a-buffer-inside-polygons-shrink-polygons-negative-buffer" shash="dMxDqzMjnDt3t41fAisD66d6HQLXr4QVm05nPikBHP7c1DHrPCOvmGDqMGMP0JnOCIW6h4AgQ24kVq/f+CF0JzNqElsCV797H2JvYW19XuedJoBbdV3kpAWV2IT6hPBowIcsplDRGmFMKvzgCZohQbzTaVFmF5asz6Ev4pDTKiA=" rel="noreferrer" target="_blank">
https://gis.stackexchange.com/questions/392505/can-i-use-r-to-do-a-buffer-inside-polygons-shrink-polygons-negative-buffer</a>). However, it turn out to be as long as I provide a negative value, the output will be an empty geometry:<br>
><br>
> library(sf)<br>
> library(spDataLarge)<br>
> st_geometry(bristol_zones[1, ]) -> a    # a is the exemplary data set<br>
><br>
>> a<br>
> Geometry set for 1 feature<br>
> Geometry type: MULTIPOLYGON<br>
> Dimension:     XY<br>
> Bounding box:  xmin: -2.534502 ymin: 51.40487 xmax: -2.488435 ymax: 51.43478<br>
> Geodetic CRS:  WGS 84<br>
> MULTIPOLYGON (((-2.510462 51.42878, -2.507985 5...<br>
>> st_buffer(a, 100)<br>
> Geometry set for 1 feature<br>
> Geometry type: POLYGON<br>
> Dimension:     XY<br>
> Bounding box:  xmin: -2.536248 ymin: 51.40393 xmax: -2.486907 ymax: 51.43598<br>
> Geodetic CRS:  WGS 84<br>
> POLYGON ((-2.517834 51.43188, -2.518218 51.4318...<br>
>> st_buffer(a, -100)<br>
> Geometry set for 1 feature  (with 1 geometry empty)<br>
> Geometry type: POLYGON<br>
> Dimension:     XY<br>
> Bounding box:  xmin: NA ymin: NA xmax: NA ymax: NA<br>
> Geodetic CRS:  WGS 84<br>
> POLYGON EMPTY<br>
><br>
> 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?<br>
><br>
> Thank you, and have a great start of the week!<br>
><br>
> 叶翔 YE, Xiang<br>
> THINKING SPATIALLY<<a href="http://www.linkedin.com/in/spatialyexiang" originalsrc="http://www.linkedin.com/in/spatialyexiang" shash="t+Bm7Izxqvtad75QEMeqbbjIiqeDm8OJjNtBMHSpx1WvvMX1o1PIBa64eqENINQDVnThtX6ogVLMgdd11XUTgfZJ3XteJjRSjKazcGgtAArXdgJ3RKciPiCs1r9t0iCULev0TFlSiI8YAGGw8xGi2ac1rpasA1FS7iNvTBKQrRE=" rel="noreferrer" target="_blank">http://www.linkedin.com/in/spatialyexiang</a>>.<br>
> Ph.D. in Spatial Statistics<br>
><br>
>       [[alternative HTML version deleted]]<br>
><br>
> _______________________________________________<br>
> R-sig-Geo mailing list<br>
> <a href="mailto:R-sig-Geo@r-project.org" target="_blank">R-sig-Geo@r-project.org</a><br>
> <a href="https://stat.ethz.ch/mailman/listinfo/r-sig-geo" originalsrc="https://stat.ethz.ch/mailman/listinfo/r-sig-geo" shash="AwvKdyRS7uOssXsXSRpt7JXSkiGrzL7RFmcKvnvlwYD3MJEAXi1G9inTnXL6fBdaRwdmMsXJk5FSt6jN4tOZNNJNp2BVJyHAo/lTkolonQIhD01Ip69Bc58k7TwsAvmm59wSs2vjImXSVziL9r+JK7lwPtx1oz5ZWg2yhu4WSko=" rel="noreferrer" target="_blank">
https://stat.ethz.ch/mailman/listinfo/r-sig-geo</a><br>
><br>
-- <br>
Nick Bearman<br>
+44 (0) 7717745715<br>
<a href="mailto:nick@geospatialtrainingsolutions.co.uk" target="_blank">nick@geospatialtrainingsolutions.co.uk</a><br>
<br>
Please let me know if I can make any adjustments related to disability or neurodivergence to improve how we interact.<br>
<br>
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.<br>
<br>
_______________________________________________<br>
R-sig-Geo mailing list<br>
<a href="mailto:R-sig-Geo@r-project.org" target="_blank">R-sig-Geo@r-project.org</a><br>
<a href="https://stat.ethz.ch/mailman/listinfo/r-sig-geo" originalsrc="https://stat.ethz.ch/mailman/listinfo/r-sig-geo" shash="rm5n72hfQLhoaZKyJC3Ov9exCKBVBRMTGSW2alZo5/Xbt/IymqKNtz7347Xuag7sQWHfpxXdy714nLnaPRhCpBJonI9yTw4Br6c9XtYOmfY0DH6OVJd8UZOq8L0afnWqEthLm49U+KUVgYawQOdFQbmQiGQmVhRDlpGjsAYzuSc=" rel="noreferrer" target="_blank">https://stat.ethz.ch/mailman/listinfo/r-sig-geo</a><br>
</blockquote>
</div>
</div>
</div>
</body>
</html>