0

Update 10.11.23

Question (1) has been resolved, but answering question (2) is still in progress.

Original Post

I am trying to create buffers around (multi)linestrings representing international land borders, which I have manually derived from country polygons in the CShapes package.

Applying st_buffer() is straightforward, but the buffers that the function generates around the borders in my data are taking strange, irregular shapes. This happens to simple, straight-line borders as well as to more complex ones. It is possible to somewhat address the problem by simplifying the border (multi)linestrings, using st_simplify(), prior to buffering. But this approach is problematic because it only works for the more complex borders by making their representation inaccurate. The reprex below using two borders from my data shows everything stated above.

What I hope to find answers to are: (1) Why would the buffer around even the seemingly simple linestring take a strange shape? (2) Is there an alternative approach to create sensible buffers that does not involve simplifying the borderlines?

library(sf)
library(tmap)

## simple border

# recreate
line1 <- structure(list(structure(c(-792285.491251808, -792285.488817639, -792285.436522982, -792285.384427687, -792285.33267453, -792285.281405349, -792285.230760659, -792285.180879259, -792285.13189786, -792285.083950704, -792285.037169198, -792284.991681557, -790240.702683605, -790240.659579577, -750536.528415538, -743907.032905619, -743907.021055776, -705997.347638523, -704336.097999047, -704336.060652765, -664913.220854293, -659976.689106017, -659976.677471231, -640303.816873962, -640303.813512255, -620718.901662572, -616828.918835104, -610882.403771673, -610882.394685652, -595564.578577553, -578300.598930178, -576658.107865964, -576658.074277915, -537693.254220241, -534429.766727421, -534429.748408387, -518012.54009363, -485910.696784914, -485910.691773335, -452102.325903294, -448880.542010921, -448880.520954105, -442063.307672312, -442063.266728173, -442063.227500371, -442063.190094792, -442063.154612405, -442063.121148988, -442063.089794869, -442063.060634682, -442063.033747138, -442063.009204815, -442062.98707396, -442062.96741431, -442062.950278934, -442062.935714084, -442062.923759075, -442062.914446178, -442062.90780053, -442062.90384007, -442062.902575489, -442062.9040102, -442062.90814033, -442062.914954731, -442062.924435009, -442062.936555573, -442062.951283708, -442062.968579657, -442062.988396733, -442063.010681444, -442063.035373637, -442063.062406661, -442063.091707545, -442063.096064972, 2918748.30210906, 2918748.30212301, 2918748.2996829, 2918748.29450937, 2918748.28661659, 2918748.2760262, 2918748.26276722, 2918748.24687599, 2918748.22839606, 2918748.20737808, 2918748.18387966, 2918748.15796519, 2917511.54289283, 2917511.51528765, 2890626.4525922, 2886229.1088279, 2886229.10084542, 2860296.14740504, 2859255.91763225, 2859255.89306825, 2832044.60830091, 2828717.72270611, 2828717.71474522, 2815052.34456301, 2815052.34221764, 2801328.70929097, 2798649.81543859, 2794555.39323204, 2794555.38690149, 2783756.45528285, 2771592.78589961, 2770523.36937069, 2770523.34651698, 2742836.82623387, 2740608.56736203, 2740608.55455154, 2728852.61822133, 2705882.11145883, 2705882.10784934, 2681373.37633357, 2679115.88414793, 2679115.86898237, 2674070.35896075, 2674070.32697836, 2674070.29291262, 2674070.25685546, 2674070.21890423, 2674070.17916136, 2674070.13773414, 2674070.09473438, 2674070.05027816, 2674070.00448547, 2674069.95747993, 2674069.90938842, 2674069.86034074, 2674069.8104693, 2674069.75990871, 2674069.70879544, 2674069.65726748, 2674069.6054639, 2674069.55352455, 2674069.50158962, 2674069.44979929, 2674069.39829337, 2674069.34721089, 2674069.29668973, 2674069.24686627, 2674069.19787498, 2674069.14984812, 2674069.10291533, 2674069.05720328, 2674069.01283537, 2674068.96993136, 2674068.96421305), dim = c(74L, 2L), class = c("XY", "LINESTRING", "sfg"))), class = c("sfc_LINESTRING", "sfc"), precision = 0, bbox = structure(c(xmin = -792285.491251808, ymin = 2674068.96421305, xmax = -442062.902575489, ymax = 2918748.30212301), class = "bbox"), crs = structure(list(input = "+proj=robin +ellps=WGS84 +datum=WGS84 +units=m +no_defs", wkt = "PROJCRS[\"unknown\",\n    BASEGEOGCRS[\"unknown\",\n        DATUM[\"World Geodetic System 1984\",\n            ELLIPSOID[\"WGS 84\",6378137,298.257223563,\n                LENGTHUNIT[\"metre\",1]],\n            ID[\"EPSG\",6326]],\n        PRIMEM[\"Greenwich\",0,\n            ANGLEUNIT[\"degree\",0.0174532925199433],\n            ID[\"EPSG\",8901]]],\n    CONVERSION[\"unknown\",\n        METHOD[\"Robinson\"],\n        PARAMETER[\"Longitude of natural origin\",0,\n            ANGLEUNIT[\"degree\",0.0174532925199433],\n            ID[\"EPSG\",8802]],\n        PARAMETER[\"False easting\",0,\n            LENGTHUNIT[\"metre\",1],\n            ID[\"EPSG\",8806]],\n        PARAMETER[\"False northing\",0,\n            LENGTHUNIT[\"metre\",1],\n            ID[\"EPSG\",8807]]],\n    CS[Cartesian,2],\n        AXIS[\"(E)\",east,\n            ORDER[1],\n            LENGTHUNIT[\"metre\",1,\n                ID[\"EPSG\",9001]]],\n        AXIS[\"(N)\",north,\n            ORDER[2],\n            LENGTHUNIT[\"metre\",1,\n                ID[\"EPSG\",9001]]]]"), class = "crs"), n_empty = 0L)

# it is a linestring
class(line1)
#> [1] "sfc_LINESTRING" "sfc"

# create 80467.2 meter (50-mile) buffer 
buff1 <- st_buffer(line1, 80467.2, endCapStyle = "FLAT")

# map shows weird buffer shape 
tm_shape(buff1) +
  tm_fill() +
  tm_shape(line1) +
  tm_lines()

## problem is fixed by simplifying the line a little

# simplify by only 1 meter 
line1_simp <- st_simplify(line1, dTolerance = 1)

# create buffer again
buff1_simp <- st_buffer(line1_simp, 80467.2, endCapStyle = "FLAT")

# buffer shape is fine
tm_shape(buff1_simp) +
  tm_fill() +
  tm_shape(line1_simp) +
  tm_lines()

## complex border

# recreate 
line2 <- structure(list(structure(list(structure(c(1502042.25108891, 1502042.26041427, 1502042.27505745, 1502042.29161053, 1502042.31055995, 1502042.33185725, 1502042.35544791, 1502042.38127155, 1502042.4092621, 1502042.43934791, 1502042.47145199, 1502042.50549219, 1502042.54138138, 1502042.57902772, 1502042.61833486, 1502042.65920221, 1514736.41328685, 1519624.12248398, 1519624.16562193, 1519624.2101682, 1519624.256004, 1519624.30300707, 1519624.35105205, 1519624.40001081, 1519624.44975277, 1519624.50014526, 1519624.55105388, 1519624.60234286, 1519624.6538754, 1519624.70551406, 1519624.75712113, 1519624.80855895, 1519624.85969034, 1519624.91037893, 1519624.96048952, 1519625.00988849, 1519625.05844405, 1519625.10602673, 1519625.15250961, 1519625.19776872, 1519625.24168335, 1519625.28413638, 1519625.32501457, 1519625.36420892, 1519625.40161487, 1519625.43713267, 1519625.47066759, 1519625.50213019, 1521652.63985234, 1523334.85688992, 1528793.831984, 1531412.15494365, 1533862.35055462, 1533862.36165983, 1533862.37557394, 1533862.39225675, 1533862.41166009, 1540369.53058897, 1540369.55349643, 1540369.57919175, 1540369.60759532, 1540369.63861909, 1540369.67216694, 1540369.70813489, 1542751.0081743, 1542751.04458345, 1542751.08298495, 1542751.1232708, 1542751.16532774, 1542751.2090375, 1542751.25427718, 1542751.30091958, 1542751.34883355, 1542751.39788436, 1542751.44793409, 1542751.498842, 1557863.82022027, 1557863.87092044, 1557863.92217292, 1557863.97383869, 1557864.02577759, 1557864.0778487, 1557864.12991079, 1557864.1818226, 1557864.23344332, 1557864.2846329, 1557864.33525249, 1557864.38516476, 1557864.4342343, 1557864.48232801, 1557864.52931541, 1557864.57506904, 1560729.80594109, 1560729.85084605, 1560729.89423659, 1560729.93599225, 1560729.97599709, 1560730.01414004, 1560730.0503152, 1560730.08442213, 1560730.11636613, 1560730.14605853, 1560730.17341687, 1560730.1983652, 1561785.84599547, 1562957.25547228, 1564720.61063292, 1566297.46872978, 1566297.5197109, 1566297.56964421, 1566297.61838373, 1566297.66578697, 1566297.71171536, 1566297.75603463, 1566297.79861522, 1567829.51452078, 1567829.55351533, 1567829.59070021, 1567829.62597592, 1567829.65924808, 1567829.69042765, 1567829.71943121, 1567829.74618116, 1567829.77060591, 1567829.79264012, 1567829.81222482, 1567829.82930762, 1567829.8438428, 1567829.85579147, 1567829.86512166, 1567829.87180841, 1567829.87583382, 1567829.87718713, 1567829.8758647, 1567513.03358143, 1567513.0294818, 1567513.02261556, 1567024.92053415, 1568220.43305772, 1569979.83698969, 1575467.60106015, 1578348.56658296, 1578348.61042199, 1578348.65555924, 1578348.7018763, 1578348.74925168, 1578348.79756112, 1578348.84667791, 1578348.8964732, 1578348.94681638, 1578348.9975754, 1578349.04861713, 1578349.09980767, 1578349.15101276, 1578349.20209808, 1578349.25292963, 1578349.30337409, 1578349.35329913, 1578349.4025738, 1578349.45106885, 1578349.49865708, 1581354.46437161, 1581354.51267182, 1581354.55972419, 1581354.60539576, 1581354.64955749, 1581354.69208459, 1581354.73285689, 1581354.77175917, 1581354.80868152, 1581354.84351959, 1581354.87617496, 1581354.90655535, 1581354.93457491, 1581354.96015446, 1581354.98322173, 1581355.00371153, 1585872.01518722, 1585872.03202403, 1585872.04649667, 1585872.05856911, 1585872.06821127, 1585872.07539916, 1585872.08011487, 1585872.08234667, 1585916.45743786, 1585916.45729481, 1585916.45505325, 1585916.45071789, 1585422.8675323, 1585422.86013079, 1585422.85009071, 1585422.83743892, 1585422.8222093, 1585422.80444261, 1585422.78418642, 1584348.53589905, 1581653.68666278, 1584454.85134015, 1586344.12750239, 1589962.09749102, 1589962.14675186, 1589962.19681773, 1593338.22596497, 1593338.27077919, 1593338.31602168, 1593338.36159671, 1593338.4074079, 1601738.38933378, 1601738.44113488, 1601738.49297367, 1601738.54471083, 1601738.59620732, 1601738.64732472, 1601738.69792565, 1601738.74787412, 1601738.79703588, 1601738.84527881, 1601738.89247323, 1601738.93849232, 1601738.98321239, 1601739.02651324, 
1601739.06827851, 1601739.10839593, 1601739.14675769, 1601739.18326069, 1601739.21780682, 1601739.25030323, 1601739.28066258, 1601739.30880328, 1601739.3346497, 1601739.35813236, 1601739.37918816, 1601739.39776051, 1601739.41379949, 1602728.46766899, 1602728.48080756, 1602728.49147121, 1602728.49963277, 1602728.50527148, 1602728.50837295, 1602854.34191324, 
 1606226.60687138, 1609513.73237069, 1612446.20885696, 1612446.25477937, 1612446.30168507, 1612446.34945507, 1616035.82630675, 1616035.87755804, 1616035.92949306, 1616035.98196489, 1616036.03482509, 1616036.08792411, 1616036.14111175, 1616036.19423753, 1616036.24715116, 1616036.29970295, 1616036.35174423, 1630700.11818811, 1630700.16584657, 1630700.21282392, 1630700.25900596, 1630700.30428041, 1630700.3485372, 1635808.39119808, 
1635808.4339838, 1635808.47556015, 1635808.51582768, 1635808.55469009, 1635808.59205441, 1638562.52443534, 1638562.57076121, 1638562.61419968, 1640701.6444635, 1640701.66843629, 1642281.81252158, 1654540.98744391, 1668948.44157856, 1668948.4718766, 1703848.19593652, 1703848.24503292, 1703848.29436465, 1703848.34381104, 1703848.39325113, 1703848.442564, 
1703848.49162901, 1724846.57290321, 1724846.62490115, 1724846.67633429, 1724846.7270591, 1724846.77693402, 1724846.82581986, 1724846.87358021, 1724846.92008177, 1724846.96519479, 1724847.00879335, 1726508.95652669, 1726508.99945003, 1726509.04053439, 1726509.0796597, 1726509.1167116, 1726509.1515818, 1726509.18416838, 1726509.21437612, 1726509.24211671, 
1726509.26730908, 1726509.2898796, 1727759.33280471, 1729171.30709679,  1738625.28167795, 1742072.49183073, 1745627.92900657, 1752193.40404745, 1752977.44179517, 1752012.9909856, 1752012.97207137, 1752012.95633004, 1752012.94381397, 1751411.67382178, 1751411.66506276, 1751411.65919409, 1751411.65623288, 1751411.65618778, 1751411.65905893, 1751411.66483793, 
1751411.67350795, 1751411.68504367, 1751411.69941146, 1751411.71656939, 1753661.11452262, 1753661.13320095, 1753661.15426443, 1753661.17765849, 1753661.20332252, 1753661.23119003, 1753661.26118881, 1753661.29324114,1753661.32726398, 1753661.36316919, 1755185.62266587, 1755185.66299419, 1755185.70524498, 1755185.74929333, 1755185.79500902, 1755185.8422569, 
1755185.8908973, 1755185.94078641, 1755185.99177677, 1755186.04371762, 1757416.20407742, 1757416.25796532, 1757416.31251878, 1757416.36756946, 1757416.42294749, 1757416.47848199, 1757416.53400159, 1757416.58933498, 1759328.68812546, 1759328.73635542, 1759328.78419651, 1759328.8315352, 1759328.87825921, 1759328.92425766, 1761700.91314776, 1768097.95812346, 
 1770820.67776963, 1772573.28768464, 1774944.7660143, 1775422.15963991, 1775422.16781573, 1775422.17891076, 1775422.19289209, 1775422.20971826, 1775422.22933934, 1775422.25169716, 1777389.73437699, 1777389.75563765, 1777389.77880866, 1777389.80383965, 1777389.83067624, 1782663.30376969, 1782663.33497708, 1782663.36817574, 1782663.40328011, 1782663.44019975, 
1782663.47883953, 1782663.5190999, 1782663.56087712, 1782663.60406354, 1782663.64854789, 1782663.69421555, 1782663.74094887, 1782663.78862741, 1782663.83712834, 1798934.11545935, 1798934.16648306, 1798934.2181152, 1798934.27021275, 1798934.32263141, 1798934.37522597, 1798934.42785074, 1798934.48035995, 1798934.53260815, 1798934.58445061, 1798934.63574373, 
1803083.691545, 1805149.26001762, 1822382.42239043, 1822801.04230098, 1824319.92286585, 1825247.76124063, 1825247.76135798, 1826719.08994362, 1826719.0899942, 1827219.49825429, 1827827.16867168, 1827827.16885052,1828102.92323398, 1829521.32649183, 1832458.34185819, 1834853.47993457,1835990.71920185, 1836829.30207188, 1836829.30212371, 1837661.05389706, 
1837661.05404068, 1838305.69768539, 1839225.44377262, 1839502.31910431, 1839578.45782698, 1839578.46053344, 1839578.46598931, 1839578.47417954, 1839578.48508156, 1839578.49866529, 1839578.51489328, 1839578.53372076,1839578.55509583, 1839578.57895952, 1839578.60524604, 1839578.63388289, 1839578.6647911, 1839578.69788542, 1839578.7330746, 1839578.77026159, 
1839578.80934383, 1839578.85021356, 1839578.89275805, 1839578.93685998, 1839578.98239773, 1839579.02924572, 1839579.07727474, 1839579.12635236, 1839579.17634322, 1839579.22710947, 1839579.27851109, 1839579.33040634,1839579.38265211, 1839579.4351043, 1839579.48761828, 1839579.5142743, -3056470.26569144, -3056470.22568985, -3056470.17509321, -3056470.12728954, -3056470.08038416, -3056470.03449712, -3056469.98974585, -3056469.94624487, -3056469.90410553, -3056469.86343565, -3056469.82433932, -3056469.78691659,-3056469.75126324, -3056469.71747052, -3056469.68562489, -3056469.65580787,-3047691.46367616, -3044290.28559514, -3044290.25720165, -3044290.23107309, -3044290.20727914, -3044290.18588326, -3044290.16694251, -3044290.15050742, -3044290.13662181, -3044290.12532272, -3044290.11664029, -3044290.11059767, -3044290.10721097, -3044290.10648924, -3044290.10843439, -3044290.11304124, -3044290.1202975, -3044290.13018382, -3044290.14267382, -3044290.15773421,-3044290.17532481, -3044290.1953987, -3044290.21790235, -3044290.24277574, -3044290.26995252, -3044290.29936023, -3044290.33092042, -3044290.36454892, -3044290.40015604, -3044290.43764681, -3044290.47692125, -3044290.51787461, -3047075.13284087, -3048003.12017369, -3041869.56927793, -3033432.08511778, -3020062.9567887, -3020062.90420577, -3020062.8522954, -3020062.8012075, -3020062.75108964, -3004509.97228864, -3004509.92155167, -3004509.87216806, -3004509.82429083, -3004509.77806836, -3004509.73364389, -3004509.6911551, -3001850.45002021, -3001850.41146893, -3001850.37490184, -3001850.34042174, -3001850.30812559, -3001850.2781042, -3001850.25044199, -3001850.22521673, -3001850.20249936, -3001850.18235375, -3001850.16483656, -3001850.14999702, -2997876.48540472, -2997876.47347602, -2997876.46420331, -2997876.45761172, -2997876.45371916, -2997876.45253616, -2997876.45406596, -2997876.45830439, -2997876.46523996, -2997876.47485385, -2997876.48711998, -2997876.50200508,-2997876.51946877, -2997876.53946367, -2997876.56193553, -2997876.5868234,-2999533.259344, -2999533.28691154, -2999533.31680612, -2999533.34894473, -2999533.38323814, -2999533.41959114, -2999533.45790279, -2999533.49806674, -2999533.53997146, -2999533.58350061, -2999533.62853333, -2999533.6749446, -3001627.99375889, -3003944.74235452, -3005058.74885262, -3005568.22767215, -3005568.2456801, -3005568.26641718, -3005568.28982276, -3005568.31582842, -3005568.34435813, -3005568.3753285, -3005568.40864898, -3006835.25165761, -3006835.28564516, -3006835.32160366, -3006835.35943689, -3006835.39904361, -3006835.44031785, -3006835.48314917, -3006835.52742297, -3006835.57302078, -3006835.61982059, -3006835.66769718, -3006835.71652245, -3006835.76616575, -3006835.81649425, -3006835.86737329, -3006835.91866672, -3006835.9702373, -3006836.02194704, -3006836.07365758, -3012990.73468293, -3012990.78726516, -3012990.83955811, -3016075.57235531, -3018546.73347559, -3020521.03823488, -3020113.7097988, -3018271.26271431, -3018271.23623496, -3018271.21203484, -3018271.19017741, -3018271.17072003, -3018271.15371371, -3018271.13920308, -3018271.12722618, -3018271.11781445, -3018271.11099255, -3018271.1067784, -3018271.10518304, -3018271.10621065, -3018271.10985855, -3018271.11611715, -3018271.12497006, -3018271.13639404, -3018271.15035913, -3018271.16682869, -3018271.18575954, -3019556.65141257, -3019556.67361114, -3019556.69834495, -3019556.7255441, -3019556.75513173, -3019556.78702425, -3019556.82113153, -3019556.8573572, -3019556.89559889, -3019556.93574855, -3019556.97769272, -3019557.02131289, -3019557.06648579, -3019557.11308378, -3019557.1609752, -3019557.21002471, -3031233.52386676, -3031233.57083997, -3031233.61859459, -3031233.6670117, -3031233.71597076, -3031233.76534986, -3031233.81502604, -3031233.86487561, -3033476.04874138, -3033476.09455842, -3033476.14032082, -3033476.18593251, -3037651.25438372, -3037651.30559039, -3037651.35634569, -3037651.40651378, -3037651.45596035, -3037651.50455303, -3037651.55216176, 
                                                   -3040005.42304249, -3051851.93641429, -3070397.49385957, -3071133.23006414, 
                                                   -3069796.38877168, -3069796.37201298, -3069796.35783932, -3068934.81351398, 
                                                   -3068934.80316919, -3068934.79489593, -3068934.7887117, -3068934.78462956, 
                                                   -3068380.637144, -3068380.63507342, -3068380.63569022, -3068380.63899273, 
                                                   -3068380.64497209, -3068380.65361221, -3068380.66488989, -3068380.67877481, 
                                                   -3068380.69522966, -3068380.71421019, -3068380.73566542, -3068380.75953767, 
                                                   -3068380.78576277, -3068380.81427026, -3068380.8449835, -3068380.87781996, 
                                                   -3068380.91269138, -3068380.94950403, -3068380.98815899, -3068381.02855236, 
                                                   -3068381.07057557, -3068381.11411569, -3068381.15905568, -3068381.20527478, 
                                                   -3068381.25264876, -3068381.30105029, -3068381.3503493, -3071712.55038966, 
                                                   -3071712.59911519, -3071712.64844151, -3071712.698243, -3071712.74839281, 
                                                   -3071712.79876324, -3075181.5788416, -3077409.78206413, -3076971.32254661, 
                                                   -3075560.50262718, -3075560.48194137, -3075560.463594, -3075560.44763163, 
                                                   -3074460.7022291, -3074460.6880061, -3074460.67652822, -3074460.66782794, 
                                                   -3074460.66192985, -3074460.65885066, -3074460.65859906, -3074460.66117578, 
                                                   -3074460.66657352, -3074460.67477701, -3074460.68576304, -3077966.03495669, 
                                                   -3077966.0475992, -3077966.06257551, -3077966.07984921, -3077966.09937832, 
                                                   -3077966.12111534, -3080633.24542414, -3080633.26911348, -3080633.29486634, 
                                                   -3080633.32262113, -3080633.35231147, -3080633.38386634, -3083076.97079676, 
                                                   -3083077.01462041, -3083077.06130766, -3085528.35557641, -3085528.38410481, 
                                                   -3087481.56691622, -3091796.79160427, -3089226.90870032, -3089226.90377456, 
                                                   -3084101.50612125, -3084101.50014711, -3084101.49660777, -3084101.49551189, 
                                                   -3084101.49686215, -3084101.50065524, -3084101.50688189, -3087295.74546571, 
                                                   -3087295.75478705, -3087295.76684132, -3087295.78159486, -3087295.79900651, 
                                                   -3087295.81902769, -3087295.8416025, -3087295.86666797, -3087295.89415413, 
                                                   -3087295.92398429, -3088498.6863938, -3088498.7192616, -3088498.75440104, 
                                                   -3088498.79170943, -3088498.83107771, -3088498.87239084, -3088498.91552805, 
                                                   -3088498.96036328, -3088499.00676549, -3088499.05459905, -3088499.10372417, 
                                                   -3091425.90204847, -3093876.85934059, -3097508.47473636, -3096773.42437321, 
                                                   -3094872.49343219, -3090831.6799765, -3086123.16857838, -3083583.67013774, 
                                                   -3083583.61564963, -3083583.56016168, -3083583.50385848, -3080455.3783403, 
                                                   -3080455.32504378, -3080455.27135209, -3080455.21742186, -3080455.16341041, 
                                                   -3080455.1094753, -3080455.05577388, -3080455.00246282, -3080454.94969762, 
                                                   -3080454.89763222, -3080454.84641851, -3074297.98995389, -3074297.94260418, 
                                                   -3074297.89626625, -3074297.85106014, -3074297.80710297, -3074297.76450865, 
                                                   -3074297.72338751, -3074297.68384611, -3074297.64598689, -3074297.60990794, 
                                                   -3072842.01632101, -3072841.97985328, -3072841.94563136, -3072841.91375642, 
                                                   -3072841.88432268, -3072841.85741717, -3072841.83311942, -3072841.81150127, 
                                                   -3072841.79262661, -3072841.77655126, -3072217.46243405, -3072217.44895077, 
                                                   -3072217.43848055, -3072217.43105572, -3072217.42669918, -3072217.42542438, 
                                                   -3072217.42723525, -3072217.4321262, -3072440.11873376, -3072440.12554499, 
                                                   -3072440.13469665, -3072440.14616704, -3072440.15992894, -3072440.17594969, 
                                                   -3073331.66282238, -3071519.3527151, -3070657.85547961, -3069468.87972878, 
                                                   -3066186.59663074, -3062339.38801887, -3062339.33417474, -3062339.28085556, 
                                                   -3062339.22821948, -3062339.17642262, -3062339.12561861, -3062339.07595813, 
                                                   -3058268.83669504, -3058268.79520863, -3058268.75475813, -3058268.71543145, 
                                                   -3058268.67731406, -3051137.58786904, -3051137.54783534, -3051137.50943681, 
                                                   -3051137.47277238, -3051137.43793651, -3051137.40501896, -3051137.37410456, 
                                                   -3051137.34527294, -3051137.31859841, -3051137.29414968, -3051137.27198976, 
                                                   -3051137.25217575, -3051137.23475868, -3051137.21978345, -3046562.51512605, 
                                                   -3046562.50221846, -3046562.49201325, -3046562.48453868, -3046562.47981547, 
                                                   -3046562.47785669, -3046562.47866776, -3046562.48224646, -3046562.48858285, 
                                                   -3046562.49765939, -3046562.50945093, -3047632.01186706, -3047305.31804708, 
                                                   -3039079.82821083, -3032566.02036418, -3001583.69183921, -2980936.468347, 
                                                   -2980936.46580772, -2949951.40783196, -2949951.40677866, -2939642.64678573, 
                                                   -2927200.89817155, -2927200.89463819, -2921937.22006573, -2890923.07904795, 
                                                   -2824499.4322145, -2766928.23573109, -2739727.8630471, -2718179.76292482, 
                                                   -2718179.76161509, -2697504.63921946, -2697504.63579549, -2682740.43219663, 
                                                   -2659122.77796432, -2651725.0062081, -2648716.22525339, -2648716.17280881, 
                                                   -2648716.12057863, -2648716.06870687, -2648716.0173366, -2648715.96660948, 
                                                   -2648715.9166654, -2648715.86764209, -2648715.81967476, -2648715.77289567, 
                                                   -2648715.72743384, -2648715.68341465, -2648715.64095948, -2648715.60018541, 
                                                   -2648715.5612049, -2648715.52412543, -2648715.48904927, -2648715.45607315, 
                                                   -2648715.42528801, -2648715.39677874, -2648715.37062397, -2648715.34689582, 
                                                   -2648715.32565974, -2648715.30697428, -2648715.29089098, -2648715.2774542, 
                                                   -2648715.26670097, -2648715.25866097, -2648715.25335636, -2648715.25080177, 
                                                   -2648715.25100425, -2648715.2525086), dim = c(462L, 2L)), structure(c(1502041.89104872, 
                                                                                                                         1502042.25085613, 1502042.25108891, -3056471.91890907, -3056470.26564078, 
                                                                                                                         -3056470.26569144), dim = 3:2)), class = c("XY", "MULTILINESTRING", "sfg"))), class = c("sfc_MULTILINESTRING", "sfc"), precision = 0, bbox = structure(c(xmin = 1502041.89104872, ymin = -3097508.47473636, xmax = 1839579.5142743, ymax = -2648715.25080177), class = "bbox"), crs = structure(list(input = "+proj=robin +ellps=WGS84 +datum=WGS84 +units=m +no_defs", wkt = "PROJCRS[\"unknown\",\n    BASEGEOGCRS[\"unknown\",\n        DATUM[\"World Geodetic System 1984\",\n            ELLIPSOID[\"WGS 84\",6378137,298.257223563,\n                LENGTHUNIT[\"metre\",1]],\n            ID[\"EPSG\",6326]],\n        PRIMEM[\"Greenwich\",0,\n            ANGLEUNIT[\"degree\",0.0174532925199433],\n            ID[\"EPSG\",8901]]],\n    CONVERSION[\"unknown\",\n        METHOD[\"Robinson\"],\n        PARAMETER[\"Longitude of natural origin\",0,\n            ANGLEUNIT[\"degree\",0.0174532925199433],\n            ID[\"EPSG\",8802]],\n        PARAMETER[\"False easting\",0,\n            LENGTHUNIT[\"metre\",1],\n            ID[\"EPSG\",8806]],\n        PARAMETER[\"False northing\",0,\n            LENGTHUNIT[\"metre\",1],\n            ID[\"EPSG\",8807]]],\n    CS[Cartesian,2],\n        AXIS[\"(E)\",east,\n            ORDER[1],\n            LENGTHUNIT[\"metre\",1,\n                ID[\"EPSG\",9001]]],\n        AXIS[\"(N)\",north,\n            ORDER[2],\n            LENGTHUNIT[\"metre\",1,\n                ID[\"EPSG\",9001]]]]"), class = "crs"), n_empty = 0L)

# it is a multilinestring
class(line2)
#> [1] "sfc_MULTILINESTRING" "sfc"

# create buffer for the more complex border
buff2 <- st_buffer(line2, 80467.2, endCapStyle = "FLAT")

# map shows that the buffer again has a weird shape
tm_shape(buff2) +
  tm_fill() +
  tm_shape(line2) +
  tm_lines()

# simplify the border by 3000 meters to see if that fixes things
line2_simp <- st_simplify(line2, dTolerance = 3000)

# create buffer 
buff2_simp <- st_buffer(line2_simp, 80467.2, endCapStyle = "FLAT")

# the buffer is partially fixed but is still problematic
tm_shape(buff2_simp) +
  tm_fill() +
  tm_shape(line2_simp) +
  tm_lines()

# simplify to an enormous extent
line2_simpmore <- st_simplify(line2, dTolerance = 10000)

# create the buffer one more time
buff2_simpmore <- st_buffer(line2_simpmore, 80467.2, endCapStyle = "FLAT")

# the buffer looks good but the borderline itself is innacurate after so much simplification
tm_shape(buff2_simpmore) +
  tm_fill() +
  tm_shape(line2_simpmore) +
  tm_lines() +
  tm_shape(line2) +
  tm_lines("purple") # real border

0

1 Answer 1

0

What's happening here is that in order to draw a FLAT endcap the buffering process needs to know what direction the line is going at that point. If you look at the coordinates of your line you can see that the overall direction from first to last point has an angle (in radians) of -0.96 (about 55 degrees):

> xy = st_coordinates(line1)
> atan2(xy[1,1]-xy[74,1],xy[1,2]-xy[74,2])
[1] -0.9609838

But the angle between the first two point is:

> atan2(xy[1,1]-xy[2,1],xy[1,2]-xy[2,2])
[1] -1.576527

which is 90 degrees. So R is trying to draw the flat cap at 90 degrees. I think this is the upper left part on your diagram, and you can see the flat cap line is vertical. This then cuts out part of the rest of the buffer since its impossible to reconcile a flat cap at that angle with a buffer of the size you want to generate.

Thinning the line works because enough of the points get dropped so that the first two points are no longer at that angle, and the cap can be drawn.

I'm not sure what the "solution" is here because its not clear what you are doing this for. You could use rounded (default) buffers because they'll work "nicely", or if you expect all your line features to be near-linear then simplifying (even all the way to just a start and end point) will give rectangular flat-capped buffers.

4
  • Thank you for the helpful explanation. As for the purpose, I want to split the border buffers by the original lines so that a buffer becomes two cleanly shaped polygons, one per side of the border. I then will rasterize each of these polygons to measure things like population. Using rounded buffers does not work because then an original border line will not fully pass through a buffer polygon, preventing splitting using st_split(). And I do not want to simplify the border lines too much prior to buffering because then the polygons for "border zones" are inaccurate. Any other ideas? Thanks.
    – jccborders
    Oct 11, 2023 at 13:41
  • One idea that I tried is creating very small (1 unit) flat-ended buffers of the lines, and then creating full flat-ended buffers (in the original 80467.2 unit size) of those first buffer polygons, in the hopes that this approach would reduce the influence of points creating unwanted angles. But the result is rounded buffers that are almost identical to what would have resulted if I had just specified rounded buffers from the start.
    – jccborders
    Oct 11, 2023 at 14:54
  • Use st_buffer with singleSide=TRUE twice, once with +ve and once with -ve distance to get buffers either side of the line?
    – Spacedman
    Oct 11, 2023 at 15:31
  • I tried using an approach that involves making one-sided buffers, but this ultimately leads to the same issues: they take undesirable shapes if endCapStyle = "FLAT", and they extend in unwanted ways if endCapStyle = "ROUND".
    – jccborders
    Oct 11, 2023 at 18:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.