I have a SpatialLinesDataFrame of streets in Malawi and Shapefile of Malawi country. The problem is that the coordinates of some parts of one of the streets are located in the Malawi lake (and it's clearly not possible in the reality), so I think there are some problems between the shapefile and the SpatialLinesDataFrame. How can I correct the coordinates of the SpatialLinesDataFrame so that they can overlap correctly? The shapefile I am using for Malawi can be found here : mwi_adm_nso_20181016_SHP.zip (the name of the file I am using is mwi_admbnda_adm3_nso_20181016.shp).
Here the code to create the SpatialLinesDataframe of the street that has segments in the lake.
# download the shapefile of Malawi
mySHPM <-
shapefile(
paste(
path,
"/mwi_admbnda_adm3_nso_20181016.shp",
sep = ""
)
)
# subset to focus on the area where the street is
mySHP <- mySHPM[mySHPM$ADM2_EN %in% c("Karonga", "Rumphi"), ]
# define the SpatialLinesDataframe for the street that goes in the lake
Xline <- c(34.21031,34.21031,34.21027,34.21026,34.21023,34.21022,34.21006,34.20951,34.20848,34.20763,
34.20712,34.20624,34.20524,34.20443,34.20412,34.20409,34.20409,34.20421,34.20418,34.20414,
34.20403,34.20437,34.20503,34.20548,34.20598,34.20625,34.20589,34.20493,34.20467,34.20449,
34.20376,34.20349,34.20377,34.20444,34.20598,34.20694,34.20912,34.20978,34.20999,34.20985,
34.20950,34.20864,34.20822,34.20820,34.20838,34.20890,34.20905,34.20876,34.20798,34.20697,
34.20599,34.20399,34.20219,34.20074,34.19889,34.19631,34.19428,34.19126,34.18883,34.18757,
34.18614,34.18343,34.18179,34.17895,34.17602,34.17429,34.17208,34.17101,34.16972,34.16955,
34.16949,34.16944,34.16954,34.16986,34.16973,34.16935,34.16908,34.16896,34.16874,34.16783,
34.16684,34.16574,34.16509,34.16471,34.16463,34.16445,34.16422,34.16420,34.16399,34.16386,
34.16358,34.16319,34.16275,34.16217,34.16153,34.16145,34.16154,34.16207,34.16210,34.16196,
34.16145,34.16095,34.16044,34.15993,34.15940,34.15889,34.15835,34.15780,34.15720,34.15652,
34.15592,34.15546,34.15524,34.15497,34.15463,34.15426,34.15387,34.15335,34.15306,34.15270,
34.15222,34.15185,34.15141,34.15107,34.15080,34.15071,34.15080,34.15120,34.15137,34.15167,
34.15143,34.15091,34.15038,34.14988,34.14946,34.14922,34.14899,34.14851,34.14815,34.14800,
34.14762,34.14732,34.14697,34.14715,34.14691,34.14640,34.14608,34.14568,34.14541,34.14543,
34.14590,34.14617,34.14634,34.14649,34.14670,34.14647,34.14623,34.14614,34.14628,34.14649,
34.14633,34.14608,34.14591,34.14556,34.14515,34.14494,34.14472,34.14444,34.14414,34.14378,
34.14354,34.14322,34.14309,34.14320,34.14355,34.14380,34.14419,34.14451,34.14485,34.14522,
34.14538,34.14530,34.14525,34.14522,34.14522,34.14520,34.14521,34.14487,34.14455,34.14419,
34.14378,34.14341,34.14304,34.14306,34.14355,34.14411,34.14461,34.14485,34.14480,34.14468,
34.14448,34.14448,34.14438,34.14445,34.14449,34.14443,34.14419,34.14395,34.14341,34.14293,
34.14259,34.14268,34.14319,34.14367,34.14371,34.14401,34.14402,34.14379,34.14326,34.14281,
34.14225,34.14182,34.14130,34.14082,34.14021,34.14002,34.13975,34.13975,34.14023,34.14068,
34.14111,34.14147,34.14145,34.14144,34.14097,34.14035,34.13982,34.13912,34.13863,34.13819,
34.13769,34.13761,34.13751,34.13746,34.13699,34.13661,34.13670,34.13716,34.13736,34.13718,
34.13663,34.13655,34.13655,34.13662,34.13683,34.13707,34.13728,34.13741,34.13740,34.13761,
34.13824,34.13843,34.13842,34.13796,34.13723,34.13716,34.13718,34.13709,34.13689,34.13702,
34.13744,34.13761,34.13754,34.13755,34.13722,34.13682,34.13665,34.13634,34.13631,34.13623,
34.13625,34.13639,34.13652,34.13656,34.13695,34.13717,34.13716,34.13694,34.13683,34.13632,
34.13584,34.13532,34.13487,34.13437,34.13384,34.13348,34.13303,34.13258,34.13235,34.13206,
34.13204,34.13173,34.13134,34.13130,34.13102,34.13098,34.13089,34.13066,34.13052,34.13049,
34.13005,34.13001,34.12955,34.12952,34.12938,34.12859,34.12828,34.12784,34.12704,34.12610,
34.12483,34.12405,34.12284,34.12198,34.12105,34.11939,34.11927,34.11853,34.11828,34.11751,
34.11658,34.11550,34.11403,34.11364,34.11339,34.11303,34.11240,34.11173,34.11087,34.11034,
34.10987,34.10982,34.11021,34.11024,34.11016,34.10976,34.10923,34.10895,34.10898,34.10904,
34.10887,34.10870,34.10847,34.10803,34.10774,34.10691,34.10652,34.10620,34.10544,34.10540,
34.10547,34.10630,34.10641,34.10682,34.10677,34.10665,34.10664,34.10727,34.10819,34.10943,
34.11040,34.11054,34.11062,34.11087,34.11081,34.11079,34.11079)
Yline <-
c(-10.45258, -10.45259, -10.45294, -10.45369, -10.45489, -10.45648, -10.45859, -10.46091, -10.46452, -10.46747,
-10.46972, -10.47282, -10.47625, -10.47942, -10.48181, -10.48305, -10.48489, -10.48688, -10.49033, -10.49375,
-10.49606, -10.49860, -10.50286, -10.50589, -10.50926, -10.51182, -10.51374, -10.51642, -10.51806, -10.52037,
-10.52250, -10.52405, -10.52527, -10.52634, -10.52831, -10.52905, -10.53041, -10.53148, -10.53272, -10.53478,
-10.53576, -10.53710, -10.53811, -10.53825, -10.53968, -10.54224, -10.54315, -10.54454, -10.54563, -10.54692,
-10.54783, -10.54895, -10.54994, -10.55071, -10.55176, -10.55325, -10.55491, -10.55747, -10.55951, -10.56054,
-10.56163, -10.56337, -10.56472, -10.56799, -10.57132, -10.57269, -10.57448, -10.57609, -10.57886, -10.58046,
-10.58276, -10.58438, -10.58613, -10.58850, -10.59053, -10.59210, -10.59307, -10.59355, -10.59357, -10.59333,
-10.59312, -10.59287, -10.59279, -10.59252, -10.59238, -10.59253, -10.59336, -10.59393, -10.59408, -10.59396,
-10.59353, -10.59313, -10.59288, -10.59271, -10.59255, -10.59260, -10.59274, -10.59306, -10.59316, -10.59317,
-10.59296, -10.59305, -10.59316, -10.59342, -10.59353, -10.59358, -10.59341, -10.59330, -10.59337, -10.59367,
-10.59402, -10.59435, -10.59463, -10.59494, -10.59532, -10.59560, -10.59559, -10.59548, -10.59519, -10.59497,
-10.59484, -10.59461, -10.59478, -10.59503, -10.59486, -10.59503, -10.59509, -10.59530, -10.59564, -10.59584,
-10.59583, -10.59578, -10.59600, -10.59620, -10.59639, -10.59614, -10.59587, -10.59588, -10.59584, -10.59549,
-10.59499, -10.59452, -10.59414, -10.59381, -10.59365, -10.59368, -10.59346, -10.59331, -10.59317, -10.59330,
-10.59354, -10.59390, -10.59430, -10.59461, -10.59489, -10.59503, -10.59519, -10.59505, -10.59496, -10.59479,
-10.59466, -10.59449, -10.59425, -10.59387, -10.59365, -10.59346, -10.59352, -10.59341, -10.59355, -10.59355,
-10.59316, -10.59312, -10.59314, -10.59323, -10.59332, -10.59374, -10.59384, -10.59372, -10.59391, -10.59422,
-10.59455, -10.59488, -10.59493, -10.59490, -10.59490, -10.59477, -10.59437, -10.59416, -10.59399, -10.59421,
-10.59427, -10.59405, -10.59378, -10.59391, -10.59426, -10.59459, -10.59435, -10.59452, -10.59462, -10.59500,
-10.59540, -10.59564, -10.59566, -10.59519, -10.59477, -10.59469, -10.59481, -10.59492, -10.59478, -10.59458,
-10.59454, -10.59468, -10.59489, -10.59522, -10.59558, -10.59608, -10.59612, -10.59612, -10.59556, -10.59541,
-10.59550, -10.59559, -10.59540, -10.59518, -10.59522, -10.59529, -10.59534, -10.59547, -10.59551, -10.59564,
-10.59592, -10.59610, -10.59623, -10.59621, -10.59598, -10.59587, -10.59586, -10.59580, -10.59568, -10.59572,
-10.59554, -10.59546, -10.59492, -10.59453, -10.59403, -10.59389, -10.59357, -10.59333, -10.59308, -10.59286,
-10.59283, -10.59279, -10.59239, -10.59197, -10.59184, -10.59131, -10.59120, -10.59101, -10.59062, -10.59048,
-10.59021, -10.59022, -10.59011, -10.59019, -10.59054, -10.59093, -10.59098, -10.59106, -10.59102, -10.59058,
-10.59023, -10.59009, -10.59001, -10.59002, -10.59028, -10.59057, -10.59101, -10.59129, -10.59150, -10.59152,
-10.59151, -10.59108, -10.59072, -10.59039, -10.59010, -10.58976, -10.58972, -10.58904, -10.58882, -10.58845,
-10.58838, -10.58833, -10.58859, -10.58851, -10.58845, -10.58817, -10.58790, -10.58771, -10.58768, -10.58799,
-10.58799, -10.58738, -10.58720, -10.58723, -10.58757, -10.58774, -10.58812, -10.58875, -10.58917, -10.58920,
-10.58930, -10.58927, -10.58915, -10.58919, -10.58933, -10.58968, -10.58981, -10.58970, -10.58913, -10.58871,
-10.58863, -10.58875, -10.58869, -10.58857, -10.58840, -10.58776, -10.58771, -10.58709, -10.58688, -10.58693,
-10.58698, -10.58680, -10.58643, -10.58626, -10.58621, -10.58628, -10.58687, -10.58748, -10.58799, -10.58860,
-10.58908, -10.58929, -10.59015, -10.59142, -10.59176, -10.59204, -10.59191, -10.59216, -10.59234, -10.59253,
-10.59294, -10.59297, -10.59298, -10.59285, -10.59288, -10.59313, -10.59308, -10.59325, -10.59455, -10.59463,
-10.59521, -10.59622, -10.59643, -10.59773, -10.59809, -10.59836, -10.59890, -10.60086, -10.60199, -10.60337,
-10.60447, -10.60461, -10.60458, -10.60432, -10.60430, -10.60432, -10.60433)
coords <- as.data.frame(cbind(x = Xline, y = Yline))
Slo1 <- Line(coords)
Sli1 <- Lines(list(Slo1),ID="ID")
HMline <- SpatialLines(list(Sli1))
class(HMline)
proj4string(HMline) <- proj4string(mySHP)
plot(mySHP)
plot(HMline, add = T, col = "red")
# you can notice a small segment going outside the shapefile. if you can't, look at this:
# crop the street to save in "cropped" just the segments that are not overlapping the shapefile
cropped <- gDifference(HMline, mySHP)
plot(HMline)
plot(cropped, add = T, col = "red", lwd = 4)
Thanks for your help!
Related
I am new to extracting data from Geotiff files.So this question will sound obvious.I have tiff file which I have got from api.It has 12 bands.This shows definition of what each band contains
What I want
After extenisve searching for days i am only able to do this.
enter image description here
Code which i have tried
driver=gdal.GetDriverByName('GTiFF')
driver.Register()
ds = gdal.Open('...../286d7a11-4abd-449e-856b-79959dfd1396.tif')
if ds is None:
print('Could not open')
geotransform = ds.GetGeoTransform()
proj = ds.GetProjection()
cols = ds.RasterXSize
rows = ds.RasterYSize
xmin=geotransform[0]
ymax=geotransform[3]
xmax=xmin+cols*geotransform[1]
ymin=ymax+rows*geotransform[5]
centerx=(xmin+xmax)/2
centery=(ymin+ymax)/2
bands = ds.RasterCount
band10 = ds.GetRasterBand(10)
array = band10.ReadAsArray()
map= folium.Map(location=[centery, centerx], zoom_start=7,tiles='openstreetmap')
image = folium.raster_layers.ImageOverlay(
dataimage,opacity=0.8, bounds=[[ymin,xmin],[ymax,xmax]]
)
image.add_to(map)
map.fit_bounds(map.get_bounds(), padding=(10, 10))
return map
I have a dataset with geometry through the sf package included with lat and lon information. I also have a heat map created as a result of the dataset
Overlay<-stat_density2d(aes(x = df$LONGITUDE, y = fd$LATITUDE, fill = ..density..), geom = 'tile', contour = F, alpha = .5)
Is there a way to join the heat map density color values into the point file?
st_join(Shots1B,Overlay)
gives me an error
Error in st_join.sf(Shots1B, Overlay) :
second argument should be of class sf: maybe revert the first two arguments?
Reproductive dataset to follow the process. Overlay is based on the result of this dataset here.
structure(list(LATITUDE = c(40.68358, 40.69754, 40.843464, 40.692547,
40.626457, 40.526894, 40.840775, 40.694035, 40.857365, 40.698807,
40.71402, 40.815, 40.55079, 40.655903, 40.890076, 40.650402,
40.79335, 40.72538, 40.75184, 40.649788, 40.686928, 40.712963,
40.801285, 40.633976, 40.670296, 40.66423, 40.817696, 40.668495,
40.841087, 40.70955, 40.733376, 40.700356, 40.83801, 40.66584,
40.761436, 40.74958, 40.73197, 40.76249, 40.668507, 40.638268,
40.696735, 40.870823, 40.574867, 40.866577, 40.775414, 40.84744,
40.542908, 40.78468, 40.632416, 40.714207, 40.727913, 40.854485,
40.698986, 40.841717, 40.861687, 40.691822, 40.856014, 40.83383,
40.68781, 40.642044, 40.69814, 40.64664, 40.680897, 40.760822,
40.74608, 40.626293, 40.767967, 40.673634, 40.579212, 40.57365,
40.73632, 40.619396, 40.820263, 40.601864, 40.810318, 40.666306,
40.708805, 40.826424, 40.63174, 40.727146, 40.67253, 40.702335,
40.587894, 40.67922, 40.65047, 40.836555, 40.870056, 40.579372,
40.805138, 40.85968, 40.605595, 40.819214, 40.827972, 40.66496,
40.719177, 40.748825, 40.733597, 40.54048, 40.738403, 40.68039,
40.817627, 40.751446, 40.76161, 40.689648, 40.596977, 40.63864,
40.565254, 40.655895, 40.68821, 40.71649, 40.876785, 40.86367,
40.835827, 40.793396, 40.84827, 40.656273, 40.693462, 40.66725,
40.844105, 40.651707, 40.680496, 40.834415, 40.7357, 40.771038,
40.69484, 40.785774, 40.733017, 40.709023, 40.692886, 40.620487,
40.618595, 40.803787, 40.82319, 40.680088, 40.827927, 40.66895,
40.879055, 40.67043, 40.875874, 40.675037, 40.767582, 40.734352,
40.63083, 40.63532, 40.714073, 40.702194, 40.764362, 40.69496,
40.79656, 40.805016, 40.66406, 40.7963, 40.66563, 40.680477,
40.737785, 40.778606, 40.75868, 40.856045, 40.880257, 40.60677,
40.695683, 40.667236, 40.8351, 40.633682, 40.698116, 40.84747,
40.8047, 40.762676, 40.7158, 40.75584, 40.772102, 40.681602,
40.62677, 40.707493, 40.8252, 40.854115, 40.768875, 40.629707,
40.72654, 40.634415, 40.66937, 40.89466, 40.669067, 40.681484,
40.82433, 40.856606, 40.65785, 40.62764, 40.58401, 40.71791,
40.780437, 40.73973, 40.7952, 40.694794, 40.614063, 40.633152,
40.612736, 40.70166, 40.80641, 40.762234, 40.863647, 40.576626,
40.60118, 40.64721, 40.681145, 40.57529, 40.786, 40.601128, 40.827923,
40.805824, 40.642776, 40.86674, 40.678375, 40.74209, 40.81228,
40.604195, 40.84383, 40.759163, 40.652927, 40.69097, 40.718864,
40.683174, 40.749744, 40.738316, 40.839382, 40.66806, 40.74715,
40.663776, 40.843903, 40.836296, 40.655285, 40.70166, 40.64606,
40.72119, 40.708363, 40.674004, 40.729176, 40.86832, 40.598515,
40.695004, 40.72773, 40.704563, 40.66807, 40.66944, 40.684082,
40.69349, 40.765266, 40.74613, 40.74708, 40.87482, 40.70399,
40.649788, 40.69507, 40.788673, 40.847897, 40.68896, 40.695377,
40.880657, 40.828114, 40.781265, 40.848736, 40.65989, 40.748436,
40.61033, 40.752556, 40.829697, 40.718826, 40.65241, 40.852673,
40.851555, 40.707928, 40.891876, 40.58947, 40.74668, 40.85814,
40.708626, 40.73464, 40.62855, 40.65563, 40.687046, 40.70326,
40.633114, 40.62046, 40.75964, 40.64254, 40.783146, 40.705452,
40.74425, 40.75348, 40.84307, 40.620914, 40.80889, 40.78847,
40.712776, 40.75868, 40.74661, 40.835705, 40.688404, 40.781715,
40.730644, 40.75218, 40.731422, 40.761234, 40.668976, 40.637276,
40.788685, 40.87356, 40.795006, 40.820095, 40.594334, 40.666306,
40.673008, 40.583626, 40.874474, 40.633995, 40.772327, 40.704937,
40.653873, 40.677917, 40.59857, 40.809563, 40.68836, 40.666737,
40.713173, 40.73006, 40.652317, 40.76122, 40.588722, 40.643456,
40.865532, 40.67612, 40.620663, 40.72166, 40.733723, 40.745686,
40.875294, 40.803555, 40.7605, 40.661995, 40.69045, 40.658672,
40.711227, 40.700485, 40.816555, 40.861862, 40.875793, 40.68657,
40.654705, 40.637054, 40.6191, 40.734566, 40.714912, 40.74734,
40.6963, 40.63598, 40.724358, 40.586277, 40.671932, 40.650703,
40.61378, 40.727375, 40.573204, 40.671604, 40.740276, 40.684,
40.704494, 40.845642, 40.82681, 40.681168, 40.662476, 40.64739,
40.687138, 40.865143, 40.866673, 40.72313, 40.674934, 40.708363,
40.739525, 40.637997, 40.750965, 40.671585, 40.694294, 40.810173,
40.694748, 40.687103, 40.861744, 40.741074, 40.67875, 40.666943,
40.6635, 40.827824, 40.575832, 40.730366, 40.640945, 40.784237,
40.76803, 40.669823, 40.659336, 40.616093, 40.763546), LONGITUDE = c(-73.97617,
-73.98312, -73.836, -73.990974, -73.918, -74.16728, -73.87246,
-73.72679, -73.84657, -73.91837, -73.74827, -73.89402, -74.20098,
-73.89817, -73.819855, -73.89422, -73.97275, -74.00011, -73.90358,
-73.9622, -73.920815, -73.93647, -73.95394, -74.02211, -73.997604,
-73.919106, -73.922615, -73.925606, -73.86447, -73.95887, -73.86665,
-73.95732, -73.87329, -73.75551, -73.76995, -73.86541, -73.78651,
-73.839584, -73.779625, -73.93187, -73.93481, -73.8721, -74.00069,
-73.8722, -73.91984, -73.89968, -74.15579, -73.80911, -73.94724,
-73.92817, -73.873245, -73.854645, -73.91671, -73.94435, -73.82435,
-73.92223, -73.91213, -73.921234, -73.9237, -73.98124, -73.89111,
-73.9246, -73.95118, -73.99832, -73.974945, -74.01572, -73.96822,
-73.89294, -73.976265, -74.11252, -73.85631, -73.969574, -73.92976,
-74.00232, -73.943634, -73.79171, -73.92577, -73.85868, -73.96793,
-73.954735, -73.798386, -73.89073, -73.95504, -73.90405, -73.917366,
-73.94306, -73.83222, -74.16948, -73.945244, -73.90427, -73.98404,
-73.84662, -73.88707, -73.82226, -73.79223, -73.96984, -73.91062,
-74.153404, -73.93864, -73.94956, -73.92366, -74.001434, -73.97076,
-73.9184, -73.97324, -74.02245, -74.1301, -73.898224, -73.96583,
-73.98484, -73.87446, -73.86741, -73.89068, -73.94043, -73.88312,
-73.90731, -73.965485, -73.88799, -73.923065, -73.93121, -73.821365,
-73.92854, -73.90377, -73.83413, -73.98391, -73.97052, -73.8852,
-73.757835, -73.832184, -74.029305, -73.99847, -73.953896, -73.889496,
-73.94398, -73.90094, -73.9339, -73.87439, -73.928185, -73.84989,
-73.930534, -73.9109, -74.00849, -73.90736, -73.95033, -73.95087,
-73.93587, -73.96162, -73.946236, -73.97226, -73.92109, -73.73846,
-73.93829, -73.73921, -73.7921, -73.93496, -73.98163, -73.87552,
-73.90079, -73.843864, -73.759575, -73.741875, -73.770004, -73.8825,
-73.89868, -73.977325, -73.89134, -73.91243, -73.954346, -73.824486,
-73.99238, -73.763954, -73.95855, -73.946884, -73.94153, -73.867714,
-73.89091, -73.94898, -73.90486, -73.71589, -74.08535, -73.89523,
-73.86137, -73.9878, -73.85049, -73.874374, -73.92841, -73.91648,
-73.89022, -73.98587, -73.95341, -73.94989, -73.70625, -73.94623,
-73.98246, -74.01393, -73.97762, -74.01172, -73.961464, -73.94227,
-73.98987, -73.8918, -73.98478, -73.99098, -74.01531, -73.7923,
-73.97655, -73.84574, -73.997086, -73.93483, -73.954636, -74.02003,
-73.92873, -73.9265, -73.984985, -73.90941, -73.97218, -73.886375,
-73.988396, -73.959335, -73.94833, -73.97483, -73.87389, -73.884315,
-73.98773, -73.84531, -73.90394, -73.985504, -73.889915, -73.927284,
-73.87369, -73.93658, -73.961464, -74.01648, -73.761185, -73.87203,
-73.81881, -73.87898, -73.83317, -73.766464, -73.9525, -73.90674,
-73.91066, -73.80789, -73.883995, -73.90864, -73.97917, -73.81517,
-73.83605, -73.98135, -73.877, -73.85589, -73.9622, -73.80114,
-73.97136, -73.92499, -73.93326, -73.94921, -73.877625, -73.93107,
-73.97599, -73.93234, -73.90536, -73.984566, -73.95932, -73.92972,
-73.91313, -73.98424, -73.9264, -73.919106, -73.952446, -73.784294,
-73.8616, -73.80105, -73.9745, -73.895744, -73.94513, -73.87421,
-73.952835, -73.92596, -73.792114, -73.86474, -73.94928, -74.07641,
-73.95817, -73.87652, -73.97833, -73.78149, -73.7334, -73.980896,
-73.848076, -73.9753, -73.95581, -73.968895, -73.90601, -73.87552,
-73.86473, -73.88875, -73.93803, -73.823845, -73.97329, -73.85201,
-73.94641, -73.96389, -73.90668, -73.93177, -73.94386, -73.81853,
-73.9485, -73.955086, -73.990944, -73.79171, -73.97851, -73.98407,
-73.90031, -73.98138, -73.94531, -73.94932, -74.008156, -73.93861,
-73.9689, -73.92923, -73.96444, -73.90224, -73.90076, -73.710754,
-73.92752, -73.93056, -73.960464, -73.972725, -73.86238, -73.936005,
-74.1524, -73.888664, -73.72516, -73.97213, -73.9088, -73.91184,
-73.95699, -73.91959, -73.959435, -73.90019, -73.72826, -73.93637,
-73.91755, -73.91282, -73.85465, -73.776146, -74.00731, -73.98643,
-74.159615, -73.72269, -73.94784, -73.88673, -73.97545, -73.915146,
-73.742516, -73.9862, -73.85165, -73.920586, -74.07225, -73.90313,
-74.09711, -73.86907, -73.92782, -73.95031, -73.81743, -73.90211,
-73.85361, -73.92941, -73.768326, -73.92314, -73.75136, -73.87204,
-73.90896, -73.90614, -73.80186, -73.92401, -73.92512, -74.02136,
-73.94027, -73.99843, -73.74868, -73.95117, -73.73427, -73.89251,
-73.911804, -73.7258, -73.794, -73.890144, -73.94276, -73.91934,
-74.12409, -73.91388, -73.94852, -73.947075, -73.87722, -73.90981,
-73.92726, -74.14523, -73.88209), COLLISION_ID = c(4407147L,
4136992L, 4395664L, 4397513L, 4403773L, 4405244L, 4405914L, 4407366L,
4407778L, 4407461L, 4407407L, 4407900L, 4407760L, 4407746L, 4408143L,
4407638L, 4407958L, 4407885L, 4407616L, 4408038L, 4408224L, 4407392L,
4407765L, 4407821L, 4407971L, 4408071L, 4407430L, 4408259L, 4407592L,
4407674L, 4407708L, 4408396L, 4407152L, 4407862L, 4407636L, 4407792L,
4407853L, 4408205L, 4407945L, 4408118L, 4408242L, 4407563L, 4408098L,
4407169L, 4407798L, 4407797L, 4407349L, 4407994L, 4408032L, 4407478L,
4407924L, 4408315L, 4407892L, 4408280L, 4408403L, 4407753L, 4408003L,
4407497L, 4408229L, 4407525L, 4407817L, 4407539L, 4408306L, 4407830L,
4407282L, 4407688L, 4407701L, 4407728L, 4408052L, 4407849L, 4407320L,
4407291L, 4408200L, 4407649L, 4407802L, 4407345L, 4408356L, 4407245L,
4408057L, 4408332L, 4407785L, 4407929L, 4407425L, 4408080L, 4408123L,
4408290L, 4408412L, 4407757L, 4407770L, 4407554L, 4407873L, 4407502L,
4408044L, 4408165L, 4407544L, 4407277L, 4407834L, 4407338L, 4407397L,
4408109L, 4407683L, 4407829L, 4407496L, 4407609L, 4407689L, 4407861L,
4407350L, 4407721L, 4407381L, 4407653L, 4407896L, 4407914L, 4407512L,
4408427L, 4407532L, 4408086L, 4407856L, 4407729L, 4407789L, 4408129L,
4407576L, 4408193L, 4407643L, 4407906L, 4407414L, 4407623L, 4407436L,
4407952L, 4407761L, 4408063L, 4407388L, 4407766L, 4407901L, 4408104L,
4407486L, 4408264L, 4407678L, 4407355L, 4408155L, 4408271L, 4408380L,
4407598L, 4407866L, 4407452L, 4407809L, 4407393L, 4407453L, 4408214L,
4407824L, 4407431L, 4407632L, 4407841L, 4407096L, 4407658L, 4407933L,
4407976L, 4407714L, 4407373L, 4407568L, 4407707L, 4407692L, 4407865L,
4407154L, 4407637L, 4407354L, 4407793L, 4407432L, 4407946L, 4407852L,
4408249L, 4407987L, 4408215L, 4408027L, 4407389L, 4407562L, 4407794L,
4407779L, 4407997L, 4407408L, 4408313L, 4407747L, 4408402L, 4407702L,
4407762L, 4407297L, 4407321L, 4408100L, 4407505L, 4408047L, 4407805L,
4407697L, 4407367L, 4407837L, 4407548L, 4407820L, 4408150L, 4407842L,
4407673L, 4407278L, 4407459L, 4407514L, 4408367L, 4407313L, 4407396L,
4407611L, 4408053L, 4407905L, 4407642L, 4407556L, 4407774L, 4407360L,
4408139L, 4408223L, 4408075L, 4407967L, 4407875L, 4407915L, 4408258L,
4408033L, 4408308L, 4407869L, 4407732L, 4407602L, 4407959L, 4408397L,
4408067L, 4408279L, 4407723L, 4408198L, 4407501L, 4408119L, 4408359L,
4407816L, 4407543L, 4407801L, 4407346L, 4407769L, 4408093L, 4407475L,
4408113L, 4407833L, 4407923L, 4407424L, 4407741L, 4407891L, 4407647L,
4407909L, 4407439L, 4407604L, 4407752L, 4407380L, 4408037L, 4408416L,
4407549L, 4408002L, 4408227L, 4408262L, 4407897L, 4408187L, 4407975L,
4408164L, 4408085L, 4408250L, 4408028L, 4407580L, 4408302L, 4407400L,
4408128L, 4408194L, 4408289L, 4407953L, 4408391L, 4408204L, 4407870L,
4407524L, 4408353L, 4407445L, 4408056L, 4407327L, 4407784L, 4407928L,
4407454L, 4407372L, 4407444L, 4407756L, 4407620L, 4407412L, 4407633L,
4407119L, 4407660L, 4408154L, 4407773L, 4407567L, 4407938L, 4407713L,
4408208L, 4408043L, 4408233L, 4407990L, 4408079L, 4408217L, 4408331L,
4407698L, 4407825L, 4408122L, 4407838L, 4407948L, 4407843L, 4407515L,
4407312L, 4407314L, 4407489L, 4407656L, 4407332L, 4407506L, 4408401L,
4407677L, 4407359L, 4408266L, 4407696L, 4407261L, 4407857L, 4407490L,
4407788L, 4408379L, 4408103L, 4408076L, 4408049L, 4407527L, 4407836L,
4408274L, 4407547L, 4407815L, 4408375L, 4407279L, 4407331L, 4407686L,
4408398L, 4408066L, 4408309L, 4408068L, 4407691L, 4407353L, 4407433L,
4407679L, 4407566L, 4408026L, 4407651L, 4407296L, 4407826L, 4407368L,
4407804L, 4407242L, 4407858L, 4407795L, 4407347L, 4408059L, 4407715L,
4408114L, 4408312L, 4407922L, 4407342L, 4407748L, 4407619L, 4408297L,
4407763L, 4408005L, 4407504L, 4408231L, 4407049L, 4408106L, 4407542L,
4407523L, 4407428L, 4408257L, 4407595L, 4407672L, 4407832L, 4407384L,
4408387L, 4407790L, 4407612L, 4407775L, 4407864L, 4407379L, 4407156L,
4407634L, 4408389L, 4407724L, 4407942L, 4408197L, 4407847L, 4407403L,
4408120L, 4407703L, 4407668L, 4407570L, 4408101L, 4407800L, 4407917L
)), row.names = c(NA, 400L), class = "data.frame")
I have two polygons
coords1[,1]
-15.99719 -16.01133 -16.04000 -16.08308 -16.14041 -16.21175 -16.29681 -16.39525 -16.50668 -16.63064 -16.76664 -16.91413 -17.07252 -17.24116 -17.41939 -17.60647 -17.80166 -18.00417 -18.21319 -18.42788 -18.64736 -18.87076 -19.09718 -19.32570 -19.55541 -19.78538 -20.01468 -20.24240 -20.46760 -20.68940 -20.90688 -21.11919 -21.32546 -21.52486 -21.71659 -21.89988 -22.07399 -22.23822 -22.39191 -22.53443 -22.66522 -22.78374 -22.88953 -22.98215 -23.06124 -23.12646 -23.17757 -23.21435 -23.23666 -23.24441 -23.23756 -23.21615 -23.18025 -23.13002 -23.06566 -22.98742 -22.89562 -22.79063 -22.67288 -22.54283 -22.40101 -22.24800 -22.08440 -21.91088 -21.72813 -21.53690 -21.33795 -21.13208-20.92012 -20.70293 -20.48137 -20.25635 -20.02876 -19.79953 -19.56958 -19.33983 -19.11120 -18.88463 -18.66102 -18.44127 -18.22626 -18.01687 -17.81393 -17.61826 -17.43066 -17.25187 -17.08262 -16.92358 -16.77540 -16.63868 -16.51396 -16.40174 -16.30249 -16.21659 -16.14440 -16.08620 -16.04224 -16.01268 -15.99764 -15.99719
coords1[,2]
6.225418 6.262789 6.300443 6.338229 6.375996 6.413591 6.450863 6.487662 6.523839 6.559250 6.593750 6.627202 6.659471 6.690426 6.719943 6.747904 6.774196 6.798712 6.821354 6.842031 6.860660 6.877166 6.891481 6.903550 6.913322 6.920758 6.925830 6.928515 6.928804 6.926696 6.922198 6.915329 6.906116 6.894597 6.880818 6.864834 6.846711 6.826520 6.804343 6.780269 6.754396 6.726828 6.697675 6.667055 6.635091 6.601912 6.567652 6.532448 6.496442 6.459780 6.422608 6.385077 6.347338 6.309542 6.271842 6.234389 6.197335 6.160829 6.125017 6.090044 6.056051 6.023174 5.991546 5.961295 5.932541 5.905401 5.879985 5.856394 5.834723 5.815060 5.797484 5.782066 5.768867 5.757941 5.749333 5.743075 5.739195 5.737707 5.738617 5.741922 5.747609 5.755654 5.766025 5.778680 5.793569 5.810631 5.829798 5.850993 5.874129 5.899115 5.925849 5.954224 5.984126 6.015434 6.048021 6.081758 6.116508 6.152130 6.188483 6.225418
coords1.sys[,1]
-17.27076 -17.28943 -17.32370 -17.37346 -17.43849 -17.51853 -17.61326 -17.72229 -17.84520 -17.98148 -18.13059 -18.29192 -18.46483 -18.64862 -18.84254 -19.04583 -19.25765 -19.47717 -19.70348 -19.93569 -20.17285 -20.41402 -20.65821 -20.90445 -21.15175 -21.39911 -21.64554 -21.89003 -22.13161 -22.36931 -22.60216 -22.82924 -23.04962 -23.26241 -23.46677 -23.66187 -23.84692 -24.02117 -24.18394 -24.33455 -24.47241 -24.59695 -24.70769 -24.80416 -24.88599 -24.95284 -25.00445 -25.04060 -25.06116 -25.06603 -25.05520 -25.02871 -24.98667 -24.92926 -24.85668 -24.76926 -24.66732 -24.55129 -24.42163 -24.27886 -24.12357 -23.95636 -23.77792 -23.58897 -23.39027 -23.18261 -22.96683 -22.74380 -22.51443 -22.27962 -22.04034 -21.79754 -21.55219 -21.30530 -21.05785 -20.81083 -20.56525 -20.32209 -20.08233 -19.84693 -19.61685 -19.39301 -19.17632 -18.96763 -18.76781 -18.57764 -18.39789 -18.22930 -18.07253 -17.92822
-17.79696 -17.67926 -17.57560 -17.48640 -17.41203 -17.35277 -17.30887 -17.28050 -17.26778 -17.27076
coords1.sys[,2]
6.465179 6.538004 6.611223 6.684541 6.757663 6.830295 6.902143 6.972919 7.042338 7.110120 7.175992 7.239689 7.300955 7.359542 7.415215 7.467750 7.516935 7.562573 7.604478 7.642483 7.676435 7.706197 7.731648 7.752688 7.769230 7.781208 7.788574 7.791299 7.789371 7.782798 7.771606 7.755841 7.735567 7.710864 7.681832 7.648589 7.611267 7.570018 7.525007 7.476416 7.424440 7.369288 7.311183 7.250359 7.187060 7.121542 7.054068 6.984910 6.914346 6.842661 6.770143 6.697084 6.623779 6.550522 6.477608 6.405332 6.333984 6.263851 6.195216 6.128355 6.063538 6.001025 5.941069 5.883910 5.829778 5.778892 5.731457 5.687664 5.647688 5.611692 5.579819 5.552199 5.528941 5.510141 5.495874 5.486197 5.481149 5.480751 5.485003 5.493890 5.507375 5.525404 5.547904 5.574784 5.605938 5.641238 5.680543 5.723695 5.770520 5.820829 5.874419 5.931076 5.990571 6.052663 6.117104 6.183634 6.251984 6.321880 6.393041 6.465179
plot(1,1,ylim=c(0,15),xlim=c(-30,-10), t="n", xlab="", ylab="")
polygon(coords1[,1], coords1[,2], border=2,col=alpha("red",0.5))
polygon(coords1.sys[,1], coords1.sys[,2], border=2,col=alpha("black",0.5))
I am trying to calculate the percent overlap, as shown using gArea and gIntersection here.
kt.small.g=data.frame(coords1[,1], coords1[,2])
names(kt.small.g)=c("x","y")
summary(kt.small.g)
kt.small.s=data.frame(coords1.sys[,1], coords1.sys[,2])
names(kt.small.s)=c("x","y")
kt.small.g_pol <- Polygons(list(Polygon(kt.small.g)), "small.g")
kt.small.s_pol <- Polygons(list(Polygon(kt.small.s)), "small.s")
kt.small.g_area <- gArea(shape["small.g"])
kt.small.intersections <- gIntersection(shape["small.g"], shape["small.s"])
but after running kt.small.g_area <- gArea(shape["small.g"]) or kt.small.intersections <- gIntersection(shape["small.g"], shape["small.s"]) I get the error:
Error in h(simpleError(msg, call)) :
error in evaluating the argument 'obj' in selecting a method for function 'is.projected': NAs not permitted in row index
I have run through the code in the linked example without error. I have checked my data for NAs, parallel structure to the example, and tried to change the names in shape[] to smaller strings or strings without "." and I still get the same error.
Thank you for your help.
Not sure what percentage you're after, but all of the areas are listed below for you to do the math on.
The following solution uses the sf library to work with the spatial objects.
If you're working with sp objects (SpatialPolygonsDataframes, etc), they can usually be changed to sf using as(x, 'sf').
library(dplyr)
library(ggplot2)
library(sf)
# Cleaning your data pasted from SO
poly1_x <- as.numeric(c("-15.99719", "-16.01133", "-16.04000", "-16.08308", "-16.14041", "-16.21175", "-16.29681", "-16.39525", "-16.50668", "-16.63064", "-16.76664", "-16.91413", "-17.07252", "-17.24116", "-17.41939", "-17.60647", "-17.80166", "-18.00417", "-18.21319", "-18.42788", "-18.64736", "-18.87076", "-19.09718", "-19.32570", "-19.55541", "-19.78538", "-20.01468", "-20.24240", "-20.46760", "-20.68940", "-20.90688", "-21.11919", "-21.32546", "-21.52486", "-21.71659", "-21.89988", "-22.07399", "-22.23822", "-22.39191", "-22.53443", "-22.66522", "-22.78374", "-22.88953", "-22.98215", "-23.06124", "-23.12646", "-23.17757", "-23.21435", "-23.23666", "-23.24441", "-23.23756", "-23.21615", "-23.18025", "-23.13002", "-23.06566", "-22.98742", "-22.89562", "-22.79063", "-22.67288", "-22.54283", "-22.40101", "-22.24800", "-22.08440", "-21.91088", "-21.72813", "-21.53690", "-21.33795", "-21.13208","-20.92012", "-20.70293", "-20.48137", "-20.25635", "-20.02876", "-19.79953", "-19.56958", "-19.33983", "-19.11120", "-18.88463", "-18.66102", "-18.44127", "-18.22626", "-18.01687", "-17.81393", "-17.61826", "-17.43066", "-17.25187", "-17.08262", "-16.92358", "-16.77540", "-16.63868", "-16.51396", "-16.40174", "-16.30249", "-16.21659", "-16.14440", "-16.08620", "-16.04224", "-16.01268", "-15.99764", "-15.99719"))
poly1_y <- c(6.225418, 6.262789, 6.300443, 6.338229, 6.375996, 6.413591, 6.450863, 6.487662, 6.523839, 6.55925, 6.59375, 6.627202, 6.659471, 6.690426, 6.719943, 6.747904, 6.774196, 6.798712, 6.821354, 6.842031, 6.86066, 6.877166, 6.891481, 6.90355, 6.913322, 6.920758, 6.92583, 6.928515, 6.928804, 6.926696, 6.922198, 6.915329, 6.906116, 6.894597, 6.880818, 6.864834, 6.846711, 6.82652, 6.804343, 6.780269, 6.754396, 6.726828, 6.697675, 6.667055, 6.635091, 6.601912, 6.567652, 6.532448, 6.496442, 6.45978, 6.422608, 6.385077, 6.347338, 6.309542, 6.271842, 6.234389, 6.197335, 6.160829, 6.125017, 6.090044, 6.056051, 6.023174, 5.991546, 5.961295, 5.932541, 5.905401, 5.879985, 5.856394, 5.834723, 5.81506, 5.797484, 5.782066, 5.768867, 5.757941, 5.749333, 5.743075, 5.739195, 5.737707, 5.738617, 5.741922, 5.747609, 5.755654, 5.766025, 5.77868, 5.793569, 5.810631, 5.829798, 5.850993, 5.874129, 5.899115, 5.925849, 5.954224, 5.984126, 6.015434, 6.048021, 6.081758, 6.116508, 6.15213, 6.188483, 6.225418)
poly2_x <- c("-17.27076 -17.28943 -17.32370 -17.37346 -17.43849 -17.51853 -17.61326 -17.72229 -17.84520 -17.98148 -18.13059 -18.29192 -18.46483 -18.64862 -18.84254 -19.04583 -19.25765 -19.47717 -19.70348 -19.93569 -20.17285 -20.41402 -20.65821 -20.90445 -21.15175 -21.39911 -21.64554 -21.89003 -22.13161 -22.36931 -22.60216 -22.82924 -23.04962 -23.26241 -23.46677 -23.66187 -23.84692 -24.02117 -24.18394 -24.33455 -24.47241 -24.59695 -24.70769 -24.80416 -24.88599 -24.95284 -25.00445 -25.04060 -25.06116 -25.06603 -25.05520 -25.02871 -24.98667 -24.92926 -24.85668 -24.76926 -24.66732 -24.55129 -24.42163 -24.27886 -24.12357 -23.95636 -23.77792 -23.58897 -23.39027 -23.18261 -22.96683 -22.74380 -22.51443 -22.27962 -22.04034 -21.79754 -21.55219 -21.30530 -21.05785 -20.81083 -20.56525 -20.32209 -20.08233 -19.84693 -19.61685 -19.39301 -19.17632 -18.96763 -18.76781 -18.57764 -18.39789 -18.22930 -18.07253 -17.92822 -17.79696 -17.67926 -17.57560 -17.48640 -17.41203 -17.35277 -17.30887 -17.28050 -17.26778 -17.27076")
poly2_x <- stringr::str_split(poly2_x, pattern = ' ') %>% unlist()
poly2_y <- c(6.465179, 6.538004, 6.611223, 6.684541, 6.757663, 6.830295, 6.902143, 6.972919, 7.042338, 7.11012, 7.175992, 7.239689, 7.300955, 7.359542, 7.415215, 7.46775, 7.516935, 7.562573, 7.604478, 7.642483, 7.676435, 7.706197, 7.731648, 7.752688, 7.76923, 7.781208, 7.788574, 7.791299, 7.789371, 7.782798, 7.771606, 7.755841, 7.735567, 7.710864, 7.681832, 7.648589, 7.611267, 7.570018, 7.525007, 7.476416, 7.42444, 7.369288, 7.311183, 7.250359, 7.18706, 7.121542, 7.054068, 6.98491, 6.914346, 6.842661, 6.770143, 6.697084, 6.623779, 6.550522, 6.477608, 6.405332, 6.333984, 6.263851, 6.195216, 6.128355, 6.063538, 6.001025, 5.941069, 5.88391, 5.829778, 5.778892, 5.731457, 5.687664, 5.647688, 5.611692, 5.579819, 5.552199, 5.528941, 5.510141, 5.495874, 5.486197, 5.481149, 5.480751, 5.485003, 5.49389, 5.507375, 5.525404, 5.547904, 5.574784, 5.605938, 5.641238, 5.680543, 5.723695, 5.77052, 5.820829, 5.874419, 5.931076, 5.990571, 6.052663, 6.117104, 6.183634, 6.251984, 6.32188, 6.393041, 6.465179)
# Make polygons as sf objects
poly1 <- data.frame(x = poly1_x, y = poly1_y) %>%
st_as_sf(coords = c('x', 'y')) %>%
st_combine() %>%
st_cast('POLYGON')
poly2 <- data.frame(x = poly2_x, y = poly2_y) %>%
st_as_sf(coords = c('x', 'y')) %>%
st_combine() %>%
st_cast('POLYGON')
# Plot
ggplot() +
geom_sf(data = poly1, fill = 'red', alpha = .5) +
geom_sf(data = poly2, fill = 'blue', alpha = .5)
# Area of polygon1: (red)
st_area(poly1)
#> [1] 6.62603
# Area of polygon2: (blue)
st_area(poly2)
#> [1] 13.88645
# Area covered only by BOTH polygons (purple)
st_intersection(poly1, poly2) %>%
st_area
#> [1] 5.678321
Created on 2020-12-30 by the reprex package (v0.3.0)
I have data frame that contains variable with set of coordinates, which supposed to be a polygon. I’d like to try to convert it into sf geometry polygon, but I have little idea how to achieve this goal.
Data looks like this:
a <- c("[30.523311, 50.40919], [30.523111, 50.409093], [30.522475, 50.408781], [30.522484, 50.408771], [30.523591, 50.407804], [30.524049, 50.407403], [30.526558, 50.406062], [30.526791, 50.405939], [30.527487, 50.4057], [30.527787, 50.405564], [30.528793, 50.405209], [30.528718, 50.404554], [30.530223, 50.404552], [30.530133, 50.404363], [30.529104, 50.404185], [30.529018, 50.403965], [30.528933, 50.403337], [30.529986, 50.403227], [30.531422, 50.403077], [30.531336, 50.402585], [30.531743, 50.402489], [30.531612, 50.401577], [30.531505, 50.401447], [30.531483, 50.401211], [30.531489, 50.40103], [30.531873, 50.400999], [30.531932, 50.400429], [30.531961, 50.400257], [30.531965, 50.400233], [30.532439, 50.400174], [30.533329, 50.400062], [30.533369, 50.399868], [30.533435, 50.399542], [30.533478, 50.39954], [30.534231, 50.399498], [30.534229, 50.399863], [30.5354583263, 50.4005476511], [30.5361664295, 50.4004929412], [30.5361825227, 50.4008211999], [30.5366814136, 50.4008656514], [30.537257, 50.400902], [30.537703, 50.400882], [30.538053, 50.400829], [30.538949, 50.40062], [30.539305, 50.400619], [30.539605, 50.40051], [30.540023, 50.40049], [30.54056, 50.400838], [30.540828, 50.400443], [30.541166, 50.400788], [30.541501, 50.401137], [30.542203, 50.40115], [30.54236, 50.40171], [30.542792, 50.401676], [30.543223, 50.401641], [30.548297, 50.400637], [30.558722, 50.398574], [30.558969, 50.398794], [30.559252, 50.399041], [30.559338, 50.399931], [30.55938, 50.400163], [30.559447, 50.400539], [30.559532, 50.400916], [30.559051, 50.400983], [30.555229, 50.401518], [30.555258, 50.402649], [30.554082, 50.402791], [30.553204, 50.402945], [30.552119, 50.40318], [30.551907, 50.403224], [30.551414, 50.403334], [30.55056, 50.403525], [30.550566, 50.403544], [30.547881, 50.404161], [30.547819, 50.404175], [30.546416, 50.404497], [30.544978, 50.404827], [30.54398, 50.404963], [30.543771, 50.404992], [30.54248, 50.405173], [30.540812, 50.405404], [30.538729, 50.405488], [30.537509, 50.405536], [30.536298, 50.405576], [30.534761, 50.405593], [30.53247, 50.405613], [30.530065, 50.40586], [30.528, 50.406381], [30.527158, 50.406595], [30.525696, 50.407249], [30.52562, 50.407313], [30.525481, 50.40743], [30.525423, 50.407478], [30.525274, 50.4076], [30.524885, 50.407919], [30.524221, 50.408445], [30.523308, 50.409165], [30.52333, 50.409175], [30.523311, 50.40919]")
b <- c("[30.517824, 50.405568], [30.517689, 50.40543], [30.517727, 50.405406], [30.517798, 50.405361], [30.517947, 50.405265], [30.518736, 50.405337], [30.519452, 50.405434], [30.520105, 50.405521], [30.52047, 50.405706], [30.520844, 50.405896], [30.521225, 50.405894], [30.521758, 50.405744], [30.524134, 50.404085], [30.524483, 50.403844], [30.524582, 50.403776], [30.524901, 50.403571], [30.524915, 50.403562], [30.524967, 50.403529], [30.525058, 50.40347], [30.525317, 50.403308], [30.525573, 50.403147], [30.526089, 50.402824], [30.526607, 50.4025], [30.527259, 50.402092], [30.527528, 50.401796], [30.528232, 50.40102], [30.528482, 50.400748], [30.528687, 50.400524], [30.528731, 50.400476], [30.528953, 50.400242], [30.529032, 50.400158], [30.529059, 50.400054], [30.529836, 50.399809], [30.530864, 50.399677], [30.530971, 50.39987], [30.53074, 50.400041], [30.53095, 50.400096], [30.53118, 50.401053], [30.531489, 50.40103], [30.531483, 50.401211], [30.531505, 50.401447], [30.531612, 50.401577], [30.531743, 50.402489], [30.531336, 50.402585], [30.531422, 50.403077], [30.529986, 50.403227], [30.528933, 50.403337], [30.529018, 50.403965], [30.529104, 50.404185], [30.530133, 50.404363], [30.530223, 50.404552], [30.528718, 50.404554], [30.528793, 50.405209], [30.527787, 50.405564], [30.527487, 50.4057], [30.526791, 50.405939], [30.526558, 50.406062], [30.524049, 50.407403], [30.523591, 50.407804], [30.522484, 50.408771], [30.519427, 50.407208], [30.519404, 50.407185], [30.519397, 50.407177], [30.518728, 50.406494], [30.518557, 50.406319], [30.517824, 50.405568]")
polygons<- as.data.frame(c(a,b), ncol=1, nrow=2)%>%
rename(polygon=1)
You need to parse these text strings into numbers, then convert into polygons. This function creates an sfc object that contains the polygons these vectors describe:
polygonise <- function(strings) {
do.call(c, lapply(strings, function(x) {
cutstring <- unlist(strsplit(x, "\\[|\\]"))
cutstring <- cutstring[nchar(cutstring) > 3]
sf::st_sfc(sf::st_polygon(list(do.call(rbind,
lapply(strsplit(cutstring, ", "), as.numeric)))))
}))
}
So, for example, we can do:
polygons$geometry <- polygonise(polygons$polygon)
ggplot(polygons) + geom_sf(aes(geometry = geometry), fill = "forestgreen")
Your solution works perfectly on the example I provided. However, when I try to use your function on real data I get an error ("'MtrxSet(x, dim, type = "POLYGON", needClosed = TRUE)': polygons not (all) closed ". Perhaps my coordinates are flawed? Sorry for that, but I’m pretty new to R.
a <- read_csv("https://raw.githubusercontent.com/slawomirmatuszak/Covid.UA/master/dzielnice.csv")
b<- polygonise(a)
I'm trying to figure out how to clip a polygon based on a linestring. I have estimated the range of a terrestrial animal and this range estimate extends into the ocean. I'd like to clip out any parts that extend into the ocean.
I'm using rnaturalearth to get the coastline, which returns a series of linestrings
library(lwgeom)
library(rnaturalearth)
library(rnaturalearthhires)
library(sf)
cl10 <- ne_coastline(scale=10,returnclass='sf')
poly1 is an sf polygon of the home range estimate. (To load this from wkt below, you need to add the final ')"', which is not displaying for some reason.)
wkt <- "SRID=4326;POLYGON((-64.73304 10.46291,-64.73375 10.46797,-64.73479 10.47601,-64.73529 10.48397,-64.73576 10.49193,-64.73613 10.49987,-64.73632 10.50779,-64.73639 10.51569,-64.73616 10.52355,-64.73585 10.5314,-64.73544 10.53924,-64.73476 10.54703,-64.73389 10.5548,-64.73279 10.56254,-64.7315 10.57025,-64.72998 10.57793,-64.7282 10.58557,-64.72634 10.5932,-64.72428 10.6008,-64.72188 10.60836,-64.71932 10.61589,-64.7167 10.62341,-64.71385 10.6309,-64.71074 10.63836,-64.70741 10.64578,-64.7061 10.64831,-64.70363 10.65314,-64.69975 10.66049,-64.6955 10.66779,-64.69131 10.67509,-64.68678 10.68235,-64.68541 10.68422,-64.6816 10.68951,-64.6763 10.69666,-64.67129 10.70384,-64.66619 10.71008,-64.6655 10.71092,-64.65921 10.71793,-64.65313 10.72497,-64.64775 10.73055,-64.64641 10.73191,-64.63922 10.7388,-64.63234 10.74572,-64.62976 10.74793,-64.62443 10.7525,-64.61667 10.7593,-64.61211 10.76288,-64.6081 10.76599,-64.59965 10.77269,-64.59471 10.77621,-64.59032 10.77927,-64.58079 10.78582,-64.57753 10.78797,-64.57081 10.79231,-64.56069 10.79877,-64.56047 10.7989,-64.54971 10.80512,-64.54362 10.80837,-64.53795 10.81135,-64.52681 10.81755,-64.52662 10.81765,-64.51376 10.82373,-64.5102 10.82536,-64.50063 10.82977,-64.49363 10.83293,-64.48692 10.83573,-64.47717 10.83971,-64.4725 10.84159,-64.46074 10.84626,-64.45774 10.8474,-64.4444 10.85221,-64.44196 10.85306,-64.42813 10.85767,-64.42525 10.8586,-64.41193 10.86269,-64.40758 10.86399,-64.39577 10.86735,-64.38903 10.86927,-64.37966 10.87175,-64.36964 10.87442,-64.36356 10.876,-64.34914 10.87941,-64.34754 10.87978,-64.33158 10.88309,-64.32668 10.88413,-64.31564 10.88629,-64.3033 10.88871,-64.29971 10.88941,-64.28386 10.89201,-64.27782 10.89299,-64.26801 10.89454,-64.25219 10.89691,-64.2509 10.89707,-64.23644 10.89877,-64.22147 10.90079,-64.22066 10.9009,-64.20497 10.90236,-64.18942 10.90414,-64.18923 10.90416,-64.17358 10.90532,-64.15793 10.90653,-64.15312 10.90687,-64.1423 10.9076,-64.12671 10.90837,-64.11307 10.90908,-64.11112 10.90917,-64.09558 10.90962,-64.08004 10.91004,-64.06644 10.91034,-64.06452 10.91038,-64.04903 10.91044,-64.03357 10.91035,-64.0181 10.91033,-64.01038 10.91024,-64.00265 10.91018,-63.98724 10.90971,-63.97184 10.90917,-63.95645 10.90859,-63.94106 10.90802,-63.93825 10.90785,-63.92572 10.90714,-63.91037 10.90623,-63.89506 10.90515,-63.87976 10.90399,-63.86445 10.9028,-63.84918 10.90147,-63.83391 10.90008,-63.81903 10.8987,-63.81864 10.89867,-63.80341 10.89699,-63.7882 10.89524,-63.77299 10.89341,-63.75779 10.89158,-63.7426 10.88963,-63.72743 10.88756,-63.71228 10.88539,-63.69713 10.88315,-63.682 10.88083,-63.66689 10.87837,-63.65179 10.87589,-63.63669 10.87336,-63.6216 10.87073,-63.61213 10.86901,-63.60653 10.86801,-63.59148 10.86514,-63.57644 10.86224,-63.56141 10.85928,-63.54637 10.85631,-63.53137 10.85314,-63.51636 10.85002,-63.50136 10.84683,-63.48638 10.84351,-63.48076 10.84219,-63.47142 10.84005,-63.45647 10.83653,-63.44153 10.83301,-63.4266 10.82935,-63.41166 10.82575,-63.39778 10.82232,-63.39674 10.82206,-63.38185 10.81815,-63.36696 10.81424,-63.35207 10.81034,-63.3372 10.80631,-63.33254 10.80498,-63.32234 10.80218,-63.30749 10.79802,-63.29265 10.79381,-63.27782 10.78953,-63.27544 10.78881,-63.263 10.78512,-63.24821 10.78062,-63.23339 10.77621,-63.22532 10.77365,-63.21862 10.77158,-63.20383 10.767,-63.18906 10.76236,-63.17885 10.759,-63.17431 10.75754,-63.15955 10.75275,-63.14481 10.74792,-63.13611 10.74489,-63.1301 10.74286,-63.11538 10.73783,-63.10067 10.73277,-63.0963 10.73121,-63.08598 10.7276,-63.07129 10.72239,-63.05827 10.71777,-63.0566 10.71719,-63.04194 10.7118,-63.02729 10.70637,-63.02302 10.70474,-63.01265 10.70089,-62.99799 10.69546,-62.98886 10.69186,-62.98338 10.68977,-62.96876 10.68412,-62.95604 10.67917,-62.95415 10.67845,-62.93956 10.67261,-62.92496 10.66686,-62.92449 10.66666,-62.91039 10.66088,-62.89582 10.65498,-62.89434 10.65436,-62.88127 10.64888,-62.86671 10.64284,-62.86525 10.6422,-62.85219 10.63661,-62.83765 10.63045,-62.83703 10.63017,-62.82315 10.62408,-62.80972 10.61828,-62.80863 10.61781,-62.79415 10.6113,-62.78359 10.60655,-62.77966 10.60483,-62.76517 10.59834,-62.75771 10.59486,-62.75071 10.5917,-62.73626 10.58505,-62.73248 10.58325,-62.72181 10.57832,-62.70751 10.57169,-62.70736 10.57162,-62.69295 10.56463,-62.68404 10.56034,-62.67853 10.55777,-62.66412 10.55078,-62.66045 10.54898,-62.64972 10.54377,-62.63741 10.53769,-62.63533 10.53669,-62.62095 10.52957,-62.61505 10.5265,-62.6066 10.52224,-62.59248 10.51528,-62.59221 10.51515,-62.57788 10.50774,-62.57134 10.50427,-62.56355 10.50026,-62.54973 10.49319,-62.54921 10.49291,-62.53488 10.48542,-62.52902 10.48223,-62.52059 10.47776,-62.50833 10.47128,-62.50628 10.4702,-62.49198 10.46256,-62.48799 10.46038,-62.47771 10.45474,-62.46826 10.44957,-62.46343 10.44698,-62.44915 10.43922,-62.44823 10.43871,-62.43489 10.43133,-62.42892 10.42796,-62.42065 10.42334,-62.40985 10.41724,-62.40641 10.41534,-62.39216 10.40741,-62.39054 10.40649,-62.37794 10.39929,-62.37191 10.39583,-62.36372 10.39116,-62.35334 10.38518,-62.34951 10.38301,-62.33529 10.37489,-62.33463 10.37451,-62.32108 10.3667,-62.31637 10.36391,-62.30691 10.3583,-62.29852 10.35336,-62.29272 10.35002,-62.28024 10.34275,-62.27852 10.34175,-62.26434 10.33344,-62.26222 10.33218,-62.25017 10.32501,-62.24453 10.32165,-62.236 10.31657,-62.2268 10.31112,-62.22183 10.3082,-62.20897 10.30057,-62.20766 10.2998,-62.19349 10.29139,-62.19126 10.29004,-62.17934 10.28291,-62.17367 10.27953,-62.16518 10.27442,-62.1561 10.26902,-62.15101 10.26602,-62.13829 10.25847,-62.13685 10.25762,-62.12268 10.24922,-62.12053 10.24792,-62.10853 10.24076,-62.10291 10.2374,-62.09437 10.2323,-62.08513 10.22685,-62.0802 10.22397,-62.06689 10.21623,-62.06601 10.21572,-62.05185 10.20731,-62.04914 10.20569,-62.03767 10.19903,-62.03092 10.19507,-62.02349 10.19073,-62.01251 10.18443,-62.00929 10.18259,-61.99507 10.17458,-61.99352 10.17369,-61.98087 10.16643,-61.97491 10.16301,-61.96667 10.15833,-61.95575 10.15225,-61.95243 10.15042,-61.93818 10.14263,-61.93598 10.1414,-61.92394 10.13476,-61.91622 10.13054,-61.90969 10.12701,-61.89539 10.11953,-61.89538 10.11953,-61.88112 10.11185,-61.87506 10.10859,-61.86685 10.10421,-61.85337 10.09744,-61.85251 10.09701,-61.8382 10.08965,-61.83157 10.08627,-61.82387 10.08238,-61.8095 10.07537,-61.80865 10.07494,-61.79516 10.06823,-61.7856 10.06359,-61.78078 10.0613,-61.76637 10.0546,-61.76101 10.052,-61.75198 10.04772,-61.73753 10.04123,-61.73543 10.04026,-61.72311 10.03459,-61.7087 10.02835,-61.70864 10.02832,-61.6942 10.02178,-61.68151 10.01636,-61.67971 10.01561,-61.66523 10.00934,-61.65243 10.00409,-61.6507 10.0034,-61.6362 9.997307,-61.62164 9.99159,-61.62152 9.991541,-61.60713 9.985571,-61.59255 9.979943,-61.59 9.978896,-61.57801 9.974096,-61.56341 9.968638,-61.55666 9.965975,-61.54884 9.962998,-61.53424 9.957559,-61.52083 9.952675,-61.51962 9.952241,-61.50503 9.94675,-61.49039 9.941541,-61.48417 9.939246,-61.47577 9.936246,-61.46114 9.930998,-61.44647 9.925975,-61.44499 9.925432,-61.43184 9.920747,-61.41718 9.9157,-61.40508 9.911503,-61.40253 9.910635,-61.38788 9.905512,-61.37319 9.900693,-61.36373 9.897351,-61.35854 9.895561,-61.34387 9.890574,-61.3292 9.885642,-61.32195 9.883129,-61.31453 9.880637,-61.29986 9.875668,-61.28519 9.870744,-61.27963 9.86882,-61.27052 9.865749,-61.25586 9.860781,-61.24118 9.855888,-61.23711 9.854474,-61.22652 9.85087,-61.21187 9.845786,-61.19718 9.840917,-61.19522 9.840219,-61.18254 9.835801,-61.16788 9.830773,-61.15422 9.826093,-61.15323 9.825755,-61.1386 9.820518,-61.12394 9.815556,-61.11459 9.812168,-61.10932 9.810305,-61.0947 9.805046,-61.08007 9.799867,-61.0761 9.798413,-61.06545 9.794565,-61.05085 9.78923,-61.03888 9.784847,-61.03624 9.783904,-61.02166 9.778431,-61.00706 9.773077,-61.00293 9.77147,-60.99249 9.767492,-60.97791 9.762033,-60.96837 9.7583,-60.96335 9.756394,-60.9488 9.750703,-60.93426 9.745196,-60.93423 9.745184,-60.91972 9.739281,-60.90518 9.733539,-60.90262 9.732464,-60.89068 9.727559,-60.87616 9.721668,-60.872 9.719885,-60.86168 9.715605,-60.84717 9.709654,-60.842 9.707398,-60.83271 9.703463,-60.81823 9.697355,-60.81315 9.695084,-60.80379 9.691013,-60.78933 9.684754,-60.78533 9.682925,-60.77491 9.678298,-60.76049 9.671856,-60.75845 9.670908,-60.7461 9.665213,-60.7321 9.65897,-60.73167 9.658779,-60.71732 9.651903,-60.70756 9.647305,-60.70294 9.645179,-60.68855 9.638528,-60.68272 9.635594,-60.67423 9.631448,-60.65988 9.624584,-60.65873 9.624011,-60.64557 9.61742,-60.63594 9.61261,-60.63126 9.610311,-60.61696 9.603115,-60.61339 9.601244,-60.60269 9.595709,-60.59153 9.589984,-60.58841 9.588402,-60.57415 9.580942,-60.57023 9.578807,-60.55993 9.573282,-60.54972 9.56775,-60.5457 9.565604,-60.53148 9.557961,-60.52921 9.556691,-60.5173 9.550029,-60.50987 9.545813,-60.50313 9.542042,-60.49044 9.534919,-60.48895 9.534081,-60.47481 9.525925,-60.47183 9.524149,-60.46069 9.517625,-60.45374 9.513459,-60.44659 9.509217,-60.43578 9.502788,-60.43248 9.50082,-60.4184 9.492318,-60.41815 9.492165,-60.40434 9.483605,-60.40132 9.481666,-60.39031 9.474767,-60.38466 9.471192,-60.3763 9.465832,-60.36857 9.460806,-60.3623 9.456769,-60.35256 9.450432,-60.34831 9.447676,-60.33671 9.440079,-60.33434 9.438494,-60.32149 9.429825,-60.3204 9.429087,-60.30646 9.419666,-60.30634 9.41958,-60.29257 9.409966,-60.2918 9.409429,-60.2787 9.400191,-60.27746 9.399307,-60.26484 9.390272,-60.26347 9.389241,-60.25107 9.379837,-60.25035 9.379307,-60.23727 9.369608,-60.23688 9.369319,-60.22353 9.359348,-60.22347 9.359308,-60.21077 9.349469,-60.20974 9.348667,-60.1981 9.339603,-60.19601 9.337965,-60.18581 9.329795,-60.18233 9.32698,-60.17361 9.320001,-60.16866 9.315952,-60.16169 9.31025,-60.15501 9.304783,-60.14981 9.300504,-60.14138 9.293462,-60.13819 9.290799,-60.12779 9.281922,-60.12688 9.28114,-60.11608 9.27156,-60.11427 9.26995,-60.10532 9.261987,-60.10077 9.257853,-60.09468 9.252432,-60.08726 9.245799,-60.08403 9.242874,-60.07381 9.2334,-60.07379 9.233381,-60.06412 9.223975,-60.06046 9.220393,-60.05442 9.214564,-60.04711 9.207419,-60.04475 9.205157,-60.03548 9.195813,-60.03381 9.194076,-60.02643 9.186502,-60.02054 9.180567,-60.01712 9.177151,-60.00845 9.167899,-60.00733 9.166661,-60.00008 9.158693,-59.9942 9.152287,-59.99151 9.149456,-59.98346 9.1403,-59.98113 9.137586,-59.97541 9.131143,-59.96807 9.122774,-59.96738 9.121988,-59.95999 9.112935,-59.95513 9.10723,-59.9521 9.103803,-59.94515 9.094816,-59.94227 9.091182,-59.93789 9.085782,-59.93084 9.076779,-59.92945 9.074836,-59.92431 9.067857,-59.91741 9.058879,-59.91674 9.057898,-59.9111 9.049991,-59.90461 9.041076,-59.90412 9.040324,-59.89875 9.032259,-59.89263 9.0234,-59.89166 9.021814,-59.88706 9.014628,-59.88132 9.005828,-59.87932 9.002556,-59.87579 8.997061,-59.87057 8.988344,-59.86712 8.982396,-59.86547 8.979644,-59.86056 8.970973,-59.85553 8.962285,-59.85506 8.96136,-59.85105 8.953681,-59.84658 8.945079,-59.84328 8.93859,-59.84219 8.93649,-59.83827 8.927974,-59.83428 8.919446,-59.8318 8.913986,-59.83032 8.910925,-59.82676 8.902464,-59.82329 8.894018,-59.82061 8.887526,-59.81977 8.885564,-59.81674 8.877187,-59.8138 8.868823,-59.81079 8.860449,-59.80996 8.857668,-59.80819 8.852137,-59.80582 8.843862,-59.80346 8.83559,-59.80117 8.827328,-59.8001 8.822896,-59.79912 8.819102,-59.79725 8.810907,-59.79569 8.802758,-59.79427 8.794631,-59.7929 8.786513,-59.79169 8.779063,-59.79157 8.7784,-59.79072 8.770362,-59.78986 8.762324,-59.78943 8.754351,-59.78887 8.746359,-59.7887 8.738428,-59.78843 8.73048,-59.78862 8.722606,-59.78885 8.714736,-59.78924 8.706892,-59.78982 8.699077,-59.79062 8.691298,-59.79158 8.683543,-59.79283 8.675833,-59.79433 8.668163,-59.796 8.660518,-59.79793 8.652914,-59.80006 8.645342,-59.80233 8.637792,-59.80473 8.630261,-59.8076 8.622805,-59.81042 8.61534,-59.81348 8.607913,-59.8168 8.600525,-59.82026 8.59316,-59.82173 8.590491,-59.82426 8.585879,-59.8284 8.57862,-59.83288 8.571415,-59.83719 8.564181,-59.84206 8.557036,-59.84255 8.556397,-59.84738 8.549962,-59.85295 8.542925,-59.85856 8.535895,-59.86182 8.531992,-59.86439 8.5289,-59.87096 8.522018,-59.87739 8.515115,-59.88035 8.512257,-59.88442 8.508307,-59.8916 8.50152,-59.89846 8.495194,-59.89894 8.49476,-59.90745 8.48818,-59.91534 8.481505,-59.91611 8.480966,-59.92454 8.475032,-59.93315 8.468469,-59.93352 8.468237,-59.94303 8.462103,-59.95068 8.457075,-59.95281 8.45572,-59.96335 8.449455,-59.96767 8.446971,-59.9744 8.44327,-59.98454 8.437626,-59.98551 8.437093,-59.99773 8.431089,-60.00122 8.429466,-60.01038 8.425151,-60.01783 8.421737,-60.02355 8.419294,-60.03434 8.414641,-60.03712 8.413498,-60.05079 8.407928,-60.05118 8.407778,-60.06655 8.402262,-60.0671 8.40207,-60.08257 8.396844,-60.08335 8.396588,-60.09931 8.391539,-60.09955 8.391468,-60.11565 8.386966,-60.11751 8.386458,-60.1317 8.382737,-60.13651 8.381502,-60.14772 8.378738,-60.15622 8.376653,-60.16369 8.375004,-60.17752 8.372051,-60.17961 8.371606,-60.19546 8.368661,-60.20054 8.367714,-60.21128 8.365889,-60.22469 8.363549,-60.2271 8.363133,-60.24281 8.361066,-60.25156 8.359805,-60.25854 8.358832,-60.27421 8.357012,-60.28009 8.356314,-60.28988 8.355213,-60.30547 8.353862,-60.31165 8.353291,-60.32107 8.352488,-60.3366 8.351552,-60.34694 8.350841,-60.35215 8.350489,-60.36761 8.349978,-60.38311 8.349196,-60.38672 8.349081,-60.39857 8.348733,-60.41396 8.34863,-60.42941 8.348205,-60.43238 8.348222,-60.44478 8.348302,-60.4601 8.348646,-60.47546 8.348801,-60.48958 8.349137,-60.49078 8.349162,-60.50607 8.349757,-60.52133 8.350551,-60.53657 8.351422,-60.55181 8.35234,-60.56705 8.353243,-60.56826 8.353347,-60.58223 8.354513,-60.5974 8.355799,-60.61255 8.357305,-60.62767 8.358933,-60.64279 8.360605,-60.6579 8.362359,-60.673 8.364125,-60.68807 8.366088,-60.70312 8.36823,-60.71816 8.370405,-60.73318 8.372689,-60.74819 8.375073,-60.76319 8.377516,-60.77817 8.380075,-60.79312 8.382838,-60.80806 8.385652,-60.823 8.388466,-60.83791 8.391453,-60.85282 8.394445,-60.86772 8.397519,-60.88259 8.400786,-60.89328 8.403199,-60.89745 8.404125,-60.91228 8.407674,-60.92711 8.411204,-60.94193 8.414806,-60.95675 8.418429,-60.97154 8.422183,-60.97937 8.424254,-60.98632 8.426057,-61.00108 8.430073,-61.01582 8.434144,-61.03057 8.43819,-61.04403 8.442022,-61.0453 8.442378,-61.06 8.446784,-61.07469 8.451253,-61.08938 8.45568,-61.09653 8.457925,-61.10405 8.460222,-61.1187 8.464963,-61.13337 8.469533,-61.14367 8.473004,-61.14799 8.474436,-61.16259 8.479449,-61.17723 8.484284,-61.18571 8.487301,-61.19181 8.489429,-61.20638 8.494624,-61.22097 8.499763,-61.2246 8.501115,-61.23552 8.505124,-61.25007 8.510523,-61.26054 8.514476,-61.2646 8.515997,-61.27911 8.521607,-61.29364 8.527151,-61.29461 8.527549,-61.30811 8.53304,-61.32262 8.538679,-61.32637 8.54027,-61.33707 8.544705,-61.35155 8.550573,-61.3567 8.552769,-61.366 8.556609,-61.38043 8.562742,-61.38571 8.565066,-61.39485 8.56896,-61.40927 8.575185,-61.41367 8.577202,-61.42366 8.581633,-61.43807 8.587938,-61.44078 8.589207,-61.45244 8.594548,-61.46683 8.600956,-61.46712 8.601093,-61.48117 8.607758,-61.49191 8.612743,-61.49553 8.614386,-61.50987 8.621131,-61.51657 8.624371,-61.52419 8.628053,-61.53852 8.634882,-61.54055 8.635894,-61.55281 8.641982,-61.56365 8.647283,-61.56712 8.648972,-61.58142 8.656042,-61.58636 8.658612,-61.59567 8.663394,-61.6089 8.669913,-61.60997 8.670443,-61.62423 8.677782,-61.63042 8.68106,-61.63846 8.68529,-61.65229 8.692257,-61.65274 8.692484,-61.66696 8.700033,-61.67293 8.703267,-61.68117 8.707694,-61.69384 8.714316,-61.69542 8.715144,-61.70964 8.722702,-61.71437 8.725309,-61.72383 8.730533,-61.73432 8.736211,-61.73804 8.738198,-61.75226 8.7458,-61.75472 8.74718,-61.76644 8.753654,-61.77439 8.758038,-61.78063 8.761465,-61.79445 8.768953,-61.79484 8.769164,-61.80901 8.777071,-61.81376 8.779755,-61.82317 8.785094,-61.83304 8.790551,-61.83736 8.792919,-61.85155 8.800729,-61.85273 8.801407,-61.86571 8.808746,-61.87173 8.812159,-61.87987 8.816762,-61.89117 8.822974,-61.89407 8.824567,-61.90826 8.832401,-61.91071 8.833805,-61.92243 8.840391,-61.9299 8.844581,-61.9366 8.848328,-61.9496 8.855433,-61.95081 8.856088,-61.96499 8.863957,-61.96915 8.86626,-61.97918 8.871832,-61.98875 8.877094,-61.99338 8.879601,-62.00761 8.887224,-62.00907 8.888036,-62.02182 8.894998,-62.02905 8.898924,-62.03603 8.902689,-62.05005 8.909962,-62.05029 8.910091,-62.06453 8.917704,-62.07048 8.920915,-62.07875 8.925356,-62.09206 8.932038,-62.09305 8.932536,-62.10732 8.939863,-62.11376 8.943178,-62.12159 8.94722,-62.13592 8.954215,-62.1364 8.954457,-62.15022 8.961379,-62.15909 8.96574,-62.16455 8.96839,-62.1789 8.975253,-62.18281 8.977177,-62.19324 8.98214,-62.20764 8.98867,-62.20798 8.988827,-62.22201 8.995404,-62.23342 9.000515,-62.23643 9.001826,-62.25085 9.008245,-62.26033 9.01242,-62.26528 9.014573,-62.27974 9.020734,-62.28868 9.024536,-62.29421 9.026849,-62.3087 9.032805,-62.31877 9.036909,-62.3232 9.038696,-62.33772 9.044497,-62.35092 9.049583,-62.35226 9.050092,-62.3668 9.055778,-62.38138 9.061171,-62.38496 9.062535,-62.39595 9.066626,-62.41054 9.071913,-62.42165 9.075877,-62.42515 9.077108,-62.43976 9.082321,-62.4544 9.087303,-62.46095 9.089601,-62.46903 9.092345,-62.4837 9.097211,-62.49837 9.101977,-62.5042 9.103905,-62.51305 9.106755,-62.52774 9.11144,-62.54244 9.116015,-62.55107 9.118738,-62.55715 9.120593,-62.57186 9.125143,-62.5866 9.12951,-62.60134 9.13385,-62.6027 9.134266,-62.61607 9.138303,-62.63081 9.142658,-62.64559 9.146787,-62.65738 9.150236,-62.66034 9.151083,-62.67508 9.1554,-62.68985 9.159601,-62.70463 9.163695,-62.71505 9.166639,-62.71941 9.167835,-62.73417 9.172075,-62.74895 9.176171,-62.76375 9.18018,-62.77516 9.183388,-62.77852 9.184314,-62.7933 9.18848,-62.80808 9.192556,-62.82288 9.196537,-62.836 9.200234,-62.83766 9.200696,-62.85243 9.204911,-62.86721 9.209034,-62.88201 9.213008,-62.89616 9.216968,-62.89679 9.217144,-62.91154 9.221455,-62.92631 9.225656,-62.94111 9.2297,-62.95345 9.233272,-62.95587 9.233962,-62.97061 9.238331,-62.98538 9.242572,-63.00014 9.246852,-63.00793 9.249151,-63.01489 9.251149,-63.02964 9.255538,-63.04439 9.259863,-63.05913 9.264312,-63.0605 9.264743,-63.07385 9.268831,-63.08858 9.273324,-63.1033 9.277867,-63.10941 9.279791,-63.11802 9.282416,-63.13273 9.287043,-63.14745 9.291602,-63.15654 9.294572,-63.16214 9.296362,-63.17684 9.301113,-63.19154 9.305801,-63.20073 9.308917,-63.20621 9.310728,-63.22088 9.315602,-63.23556 9.32047,-63.24252 9.322906,-63.25021 9.325509,-63.26486 9.330543,-63.27952 9.335516,-63.28258 9.336637,-63.29414 9.340751,-63.30879 9.345801,-63.32073 9.350086,-63.32342 9.351025,-63.33803 9.356325,-63.35264 9.361644,-63.35683 9.363232,-63.36724 9.367056,-63.38185 9.372398,-63.39172 9.376198,-63.39643 9.377946,-63.41101 9.383481,-63.4256 9.388971,-63.42577 9.389038,-63.44015 9.394689,-63.45471 9.400371,-63.45759 9.401552,-63.46925 9.406195,-63.48379 9.411991,-63.48841 9.413916,-63.49831 9.41794,-63.51286 9.42373,-63.51856 9.42618,-63.52735 9.42987,-63.54188 9.435778,-63.54756 9.438275,-63.55636 9.442068,-63.57086 9.448139,-63.57554 9.45022,-63.58533 9.45446,-63.59981 9.460716,-63.60279 9.462057,-63.61427 9.467086,-63.62873 9.47348,-63.62943 9.473802,-63.64316 9.480068,-63.65492 9.48538,-63.6576 9.486572,-63.67203 9.493176,-63.6798 9.496868,-63.68644 9.499925,-63.70085 9.506698,-63.70415 9.508277,-63.71525 9.513514,-63.72796 9.519605,-63.72964 9.520394,-63.74402 9.527342,-63.75079 9.530792,-63.75836 9.534523,-63.77274 9.541507,-63.77367 9.541982,-63.78707 9.548808,-63.79536 9.553001,-63.80142 9.556009,-63.81575 9.563295,-63.81715 9.564031,-63.83005 9.570831,-63.83765 9.574875,-63.84435 9.578359,-63.85816 9.585719,-63.85863 9.585973,-63.87291 9.593655,-63.87799 9.596464,-63.88717 9.601486,-63.89757 9.607171,-63.90143 9.609239,-63.91569 9.617038,-63.91721 9.617886,-63.92993 9.625015,-63.936 9.628477,-63.94415 9.633087,-63.95447 9.639021,-63.95837 9.641233,-63.97259 9.64932,-63.97303 9.649577,-63.98678 9.65757,-63.99082 9.660021,-64.00093 9.666158,-64.00796 9.670372,-64.0151 9.674615,-64.02515 9.680728,-64.02925 9.683219,-64.04222 9.691066,-64.0434 9.691781,-64.05754 9.700479,-64.05895 9.701355,-64.07166 9.709306,-64.07515 9.711566,-64.08574 9.718353,-64.09098 9.721724,-64.09982 9.727477,-64.1065 9.731836,-64.11388 9.736659,-64.1218 9.741916,-64.12793 9.745953,-64.13703 9.751986,-64.14198 9.75527,-64.152 9.762018,-64.15601 9.764757,-64.16645 9.771974,-64.17001 9.774424,-64.18087 9.781926,-64.184 9.784119,-64.19493 9.791825,-64.19797 9.793979,-64.20878 9.801693,-64.21193 9.803951,-64.2223 9.811514,-64.22586 9.81411,-64.23582 9.821334,-64.23979 9.82428,-64.24886 9.831085,-64.25367 9.834726,-64.26173 9.840811,-64.26756 9.84523,-64.27455 9.850529,-64.28141 9.855888,-64.28697 9.86019,-64.29526 9.866617,-64.2994 9.869852,-64.30908 9.877549,-64.31154 9.879472,-64.32289 9.888574,-64.32349 9.889065,-64.33499 9.898592,-64.33665 9.899977,-64.34649 9.90812,-64.35039 9.911459,-64.35765 9.917599,-64.36412 9.92306,-64.36883 9.92708,-64.37781 9.93489,-64.37971 9.936518,-64.39048 9.94594,-64.39149 9.946851,-64.40087 9.955307,-64.40511 9.959195,-64.41121 9.964667,-64.41873 9.971496,-64.42154 9.974026,-64.43141 9.983318,-64.43231 9.984178,-64.44103 9.992575,-64.44583 9.997226,-64.45074 10.00184,-64.45937 10.01015,-64.46035 10.0111,-64.46952 10.02029,-64.47282 10.02367,-64.47852 10.02946,-64.48627 10.03726,-64.48765 10.03864,-64.49632 10.04776,-64.49963 10.05139,-64.50479 10.05685,-64.51301 10.06547,-64.5135 10.06598,-64.52155 10.07501,-64.52628 10.08025,-64.52987 10.08408,-64.53768 10.09308,-64.53952 10.09529,-64.54538 10.10206,-64.55274 10.11049,-64.55324 10.11106,-64.56054 10.11998,-64.56584 10.12647,-64.56791 10.12892,-64.57498 10.13781,-64.5789 10.14274,-64.58219 10.14672,-64.58887 10.15556,-64.59188 10.15957,-64.59568 10.16441,-64.602 10.17319,-64.60477 10.17703,-64.60859 10.18202,-64.61467 10.19077,-64.61759 10.19498,-64.62091 10.19954,-64.62659 10.20823,-64.63029 10.2138,-64.63247 10.21695,-64.638 10.22563,-64.6429 10.23321,-64.64365 10.23431,-64.64869 10.24292,-64.65392 10.25155,-64.65532 10.25399,-64.65907 10.26016,-64.66367 10.2687,-64.66761 10.27563,-64.6686 10.27729,-64.67296 10.2858,-64.67743 10.29432,-64.67966 10.2989,-64.6817 10.30281,-64.68575 10.31127,-64.68969 10.31972,-64.69148 10.3238,-64.69352 10.32815,-64.69708 10.33654,-64.70046 10.34491,-64.70297 10.351,-64.70398 10.3533,-64.70694 10.36161,-64.70995 10.36992,-64.71297 10.37824,-64.71397 10.38155,-64.71559 10.3865,-64.71798 10.39473,-64.72045 10.40297,-64.72271 10.41118,-64.72426 10.41703,-64.72493 10.41938,-64.72683 10.42754,-64.72828 10.43564,-64.72991 10.44376,-64.73126 10.45184,-64.73266 10.45993,-64.73304 10.46291))"
poly1 <- st_sf(st_as_sfc(wkt))
I use this to extract out the relevant linestring.
box <- poly1 %>% st_bbox %>% st_as_sfc
sa_coast <- cl10[st_intersects(box,cl10,sparse=FALSE),] %>%
mutate(len=st_length(geometry)) %>%
filter(len==max(len))
From here, I want to clip poly1 using sa_coast. However I can't figure out how this can be done. Here is what the coast and range look like. The northern part of the map is ocean, the southern part is land.
ggplot() +
geom_sf(data=poly1,fill=NA,color='red') +
geom_sf(data=sa_coast) +
xlim(st_bbox(poly1)['xmin'],st_bbox(poly1)['xmax']) +
ylim(st_bbox(poly1)['ymin'],st_bbox(poly1)['ymax']) +
theme_classic()
I don't think that st_intersection() works, because this returns the multiline string that intersects with poly1
What I want is a multipolygon that has the water (top area of the figure) clipped out. See below, the geom returned by st_intersection() is in blue.
intr <- st_intersection(poly1,sa_coast)
ggplot() +
geom_sf(data=poly1,fill=NA,color='red') +
geom_sf(data=sa_coast) + #,mapping=aes(color=rowid)
geom_sf(data=intr,color='blue') +
xlim(st_bbox(polys)['xmin'],st_bbox(polys)['xmax']) +
ylim(st_bbox(polys)['ymin'],st_bbox(polys)['ymax']) +
theme_classic()
Here is the solution I finally figured out. Thanks to #JindaLacko who gave me the idea to use st_polygonize on a linestring. My linestring did not close however, so st_polygonize did not work. The trick is to clip the linestring using the polygon's bounding box, then use the min corner of the bounding box as a point on the linestring, and then close the linestring with it's first point. You can then turn this into a polygon.
Clip the coastline to size of the bounding box of the polygon.
sa_clip <- st_intersection(st_as_sfc(st_bbox(poly1)),sa_coast)
ggplot() +
geom_sf(data=st_as_sfc(st_bbox(poly1)),fill=NA) +
geom_sf(data=sa_clip) +
theme_classic()
Get the min corner of bounding box
corner <- st_bbox(poly1)[c('xmin','ymin')] %>% st_point %>% st_sfc(crs=4326)
Cast the linestring to points, append the corner, close the linestring, turn into polygon.
pts <- sa_clip %>% st_geometry %>% st_cast("POINT")
sa_poly <- c(pts,corner,pts[1]) %>% st_combine %>% st_cast('LINESTRING') %>% st_polygonize
Now, you can clip the polygon.
clipped <- st_intersection(sa_poly,poly1)
ggplot() +
geom_sf(data=sa_poly,fill='red',alpha=.2) +
geom_sf(data=poly1,fill='blue',alpha=.2) +
geom_sf(data=clipped,fill='purple',alpha=.2) +
theme_classic()
For clipping a linestring & a polygon use sf::st_intersection()
Consider the example on RStudio Community site, where I used it to determine an intersection of polygons of some random North Carolina (a popular dataset) counties with the 36th parallel.
https://community.rstudio.com/t/how-to-change-color-of-polyline-when-it-is-passing-a-polygon-in-leaflet-package-using-r/37033/3?u=jlacko
Edit: an intersection of linestring and polygon will be a linestring.
If the desired output is a polygon consider calling sf::st_polygonize() on your linestring. It will turn it into a polygon, which will return a polygon upon intersection with the other polygon.