Change SpatialLinesDataFrame coordinates to fit into a shapefile

Viewed 62

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!

0 Answers
Related