R terra cannot create SpatVector from sf MULTILINESTRING if line contains only single point

163 views Asked by At

I have an MULTILINESTRING sf that appears to be ok (see dput below), but when I try and convert it to a SpatVector using pv <- terra::vect(test2) I get the error: Error: [as,sf] coercion failed. You can try coercing via a Spatial* (sp) class

I have narrowed the problem down to the last record in the sf, which looks like a multilinestring that contains only a single point.

test2$geom results in:

Geometry set for 2 features 
Geometry type: MULTILINESTRING
Dimension:     XY
Bounding box:  xmin: 571594.1 ymin: 4495183 xmax: 581136.1 ymax: 4506163
Projected CRS: WGS 84 / UTM zone 18N
MULTILINESTRING ((571594.1 4499249, 571920 4499...
MULTILINESTRING ((572657.1 4506163))

The following sequence of tests shows that if I remove the last point it works. Curiously, using mapview on the original data seems to work, so it must be 'fixing' the issue somehow.

I basically need to do what mapview does and fix the issue or somehow 'fix' the geometry. Alternatively, if there is an easy way to just drop all the single-point lines that would work as well as they are essentially non-relevant lines.

plot(test2)  # fails
mapview(test2) # works
pv <- terra::vect(test2) #fails

plot(test2[1,]) # works
pv <- terra::vect(test2[1,]) # works
terra::plot(pv) # works
test2 <- structure(list(group_id = c(366836590, 366836590), track_id = c(3, 
4), geom = structure(list(structure(list(structure(c(571594.141614686, 
571920.040330265, 572235.373500609, 572552.939667288, 572867.044277763, 
573191.193433925, 573509.462836065, 573838.050757074, 574161.407562594, 
574492.225424091, 574810.332471992, 575094.37782502, 575411.593736768, 
575732.773665582, 576060.007470469, 576395.3886914, 576713.010013541, 
576936.192427651, 577206.508766628, 577536.54366881, 577890.017679123, 
578225.77967648, 578558.906796378, 578867.283291115, 579154.118826921, 
579423.347202717, 579662.711568493, 579864.11330182, 580065.472596524, 
580248.458382593, 580389.184043574, 580510.456561466, 580638.506465409, 
580758.012404972, 580842.435859698, 580914.744811195, 580955.493025696, 
581018.576810103, 581086.679434308, 581136.14845866, 581104.059895533, 
580968.342601881, 580893.703968293, 580868.026522497, 580811.25406253, 
580734.232597083, 580699.776436446, 580652.621537952, 580588.868758263, 
580543.824669649, 580496.983667437, 580454.686562792, 580362.3910162, 
580273.354071326, 580177.020908758, 580089.177096752, 579986.300321162, 
579800.3205345, 579553.799991467, 579278.972342547, 578921.606563803, 
578515.980239742, 578097.460164048, 577719.265303879, 577410.919734233, 
577186.813682709, 576949.681072738, 576733.540188331, 576492.370960301, 
576250.696872791, 576153.396930229, 575926.623587757, 575672.818319777, 
575418.168656901, 575178.544250056, 574947.796829678, 574725.487719849, 
574509.136366268, 574285.158611875, 574051.59791778, 573803.361371084, 
573550.664196344, 573305.477677964, 573056.205061962, 572830.141663269, 
572624.942933168, 572394.581163185, 572220.621555538, 572076.595727644, 
571969.249276564, 571896.325659601, 571871.159375604, 571878.453711587, 
571940.5530058, 572033.628823543, 572141.08084052, 572239.236789452, 
572317.866955656, 572410.070658737, 572483.779427369, 572551.739589597, 
572636.048122001, 572711.217440076, 572788.345284206, 572856.488364034, 
572927.328208324, 573050.245905834, 573148.411316291, 573177.992526236, 
573191.668463634, 573194.368040482, 573183.500963235, 573137.609777608, 
573074.422316074, 573020.963323051, 572936.573219412, 572879.305568426, 
572768.806184968, 572728.044010922, 572708.681975154, 572666.101277124, 
572671.421879909, 572667.011568259, 572659.459576201, 572652.806996672, 
572652.861277294, 572650.499535317, 572655.461849774, 572653.728134291, 
572646.045845649, 572641.841835243, 572643.488704536, 4499249.35002677, 
4499303.560981, 4499313.27772609, 4499354.11385029, 4499402.70075919, 
4499461.39124524, 4499515.59724417, 4499552.15692528, 4499604.22122189, 
4499670.80646525, 4499739.49900769, 4499831.17462889, 4499903.21467958, 
4499917.58032372, 4499918.69981788, 4499949.89186163, 4500063.06816727, 
4500149.74669233, 4500256.90434822, 4500312.51657155, 4500309.54999859, 
4500222.04075869, 4500064.57774107, 4499850.24792082, 4499595.73493116, 
4499328.83264787, 4499009.4415937, 4498682.98918531, 4498362.09237406, 
4498021.01938955, 4497678.3838472, 4497336.65053463, 4496994.99252277, 
4496662.12530777, 4496366.62483043, 4496096.52763564, 4495928.22399489, 
4495729.07867315, 4495535.53938396, 4495344.01800069, 4495182.69320745, 
4495291.12366501, 4495541.21180229, 4495724.11161475, 4495887.80102865, 
4496124.54276012, 4496337.32269119, 4496551.07538779, 4496813.49637255, 
4497067.23892643, 4497330.95392236, 4497565.85379112, 4497887.91769833, 
4498222.22980839, 4498606.42226982, 4498988.48700699, 4499353.73844377, 
4499662.59020583, 4499940.82368738, 4500099.97429922, 4500229.39373529, 
4500366.09148708, 4500443.83644494, 4500466.51762867, 4500439.98727895, 
4500362.16588306, 4500235.36916516, 4500122.11882095, 4500057.46472139, 
4500041.66061658, 4500047.32449999, 4500062.76814427, 4500075.72321024, 
4500088.67835108, 4500037.40475052, 4499944.04234031, 4499851.88218604, 
4499755.3478766, 4499659.85398137, 4499592.02545699, 4499554.03291006, 
4499539.31783167, 4499534.67676651, 4499515.57166708, 4499457.84688446, 
4499428.08653675, 4499376.99406439, 4499351.98969488, 4499379.45643872, 
4499461.67807227, 4499569.76732271, 4499723.83474343, 4499931.50311165, 
4500155.24369748, 4500410.36922577, 4500664.52550321, 4500917.48311066, 
4501178.0233485, 4501433.14610653, 4501676.98831191, 4501903.0128493, 
4502099.22376342, 4502278.69401819, 4502430.43043447, 4502549.88509296, 
4502652.71455466, 4502702.76902743, 4502777.00532239, 4502777.2966276, 
4502761.88912082, 4502745.26333519, 4502732.9445383, 4502758.02628671, 
4502822.90387317, 4502930.06361045, 4503002.50506308, 4503067.44236871, 
4503130.74831038, 4503150.3319701, 4503142.37133221, 4503088.66712912, 
4503063.18555332, 4503082.01508843, 4503076.39043284, 4503065.22379425, 
4503059.67353809, 4503041.88792303, 4503053.03802731, 4503057.46170106, 
4503065.15767431, 4503062.89625002, 4503067.35298339), .Dim = c(132L, 
2L))), class = c("XY", "MULTILINESTRING", "sfg")), structure(list(
    structure(c(572657.146189374, 4506162.61309533), .Dim = 1:2)), class = c("XY", 
"MULTILINESTRING", "sfg"))), class = c("sfc_MULTILINESTRING", 
"sfc"), precision = 0, bbox = structure(c(xmin = 571594.141614686, 
ymin = 4495182.69320745, xmax = 581136.14845866, ymax = 4506162.61309533
), class = "bbox"), crs = structure(list(input = "WGS 84 / UTM zone 18N", 
    wkt = "PROJCRS[\"WGS 84 / UTM zone 18N\",\n    BASEGEOGCRS[\"WGS 84\",\n        ENSEMBLE[\"World Geodetic System 1984 ensemble\",\n            MEMBER[\"World Geodetic System 1984 (Transit)\"],\n            MEMBER[\"World Geodetic System 1984 (G730)\"],\n            MEMBER[\"World Geodetic System 1984 (G873)\"],\n            MEMBER[\"World Geodetic System 1984 (G1150)\"],\n            MEMBER[\"World Geodetic System 1984 (G1674)\"],\n            MEMBER[\"World Geodetic System 1984 (G1762)\"],\n            MEMBER[\"World Geodetic System 1984 (G2139)\"],\n            ELLIPSOID[\"WGS 84\",6378137,298.257223563,\n                LENGTHUNIT[\"metre\",1]],\n            ENSEMBLEACCURACY[2.0]],\n        PRIMEM[\"Greenwich\",0,\n            ANGLEUNIT[\"degree\",0.0174532925199433]],\n        ID[\"EPSG\",4326]],\n    CONVERSION[\"UTM zone 18N\",\n        METHOD[\"Transverse Mercator\",\n            ID[\"EPSG\",9807]],\n        PARAMETER[\"Latitude of natural origin\",0,\n            ANGLEUNIT[\"degree\",0.0174532925199433],\n            ID[\"EPSG\",8801]],\n        PARAMETER[\"Longitude of natural origin\",-75,\n            ANGLEUNIT[\"degree\",0.0174532925199433],\n            ID[\"EPSG\",8802]],\n        PARAMETER[\"Scale factor at natural origin\",0.9996,\n            SCALEUNIT[\"unity\",1],\n            ID[\"EPSG\",8805]],\n        PARAMETER[\"False easting\",500000,\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        AXIS[\"(N)\",north,\n            ORDER[2],\n            LENGTHUNIT[\"metre\",1]],\n    USAGE[\n        SCOPE[\"Engineering survey, topographic mapping.\"],\n        AREA[\"Between 78°W and 72°W, northern hemisphere between equator and 84°N, onshore and offshore. Bahamas. Canada - Nunavut; Ontario; Quebec. Colombia. Cuba. Ecuador. Greenland. Haiti. Jamica. Panama. Turks and Caicos Islands. United States (USA). Venezuela.\"],\n        BBOX[0,-78,84,-72]],\n    ID[\"EPSG\",32618]]"), class = "crs"), n_empty = 0L)), row.names = 113:114, class = c("sf", 
"data.frame"), sf_column = "geom", agr = structure(c(group_id = NA_integer_, 
track_id = NA_integer_), .Label = c("constant", "aggregate", 
"identity"), class = "factor"))
0

There are 0 answers