Skip to content

Instantly share code, notes, and snippets.

@carsonfarmer
Created October 22, 2015 18:39
Show Gist options
  • Save carsonfarmer/e63d21dd3cf9105505db to your computer and use it in GitHub Desktop.
Save carsonfarmer/e63d21dd3cf9105505db to your computer and use it in GitHub Desktop.
Problems with line merging geometries near the anti-meridian
from shapely import wkt
from shapely import ops
import matplotlib.pyplot as plt
%matplotlib inline
a = wkt.loads("LINESTRING (-180 68.96363636363637, -177.55 68.2, -174.92825 67.20589, -175.01425 66.58435, -174.33983 66.33556, -174.57182 67.06219, -171.85731 66.91307999999999, -169.89958 65.97723999999999, -170.89107 65.54139000000001, -172.53025 65.43791, -172.555 64.46079, -172.95533 64.25269, -173.89184 64.2826, -174.65392 64.63124999999999, -175.98353 64.92288000000001, -176.20716 65.35666999999999, -177.22266 65.52024, -178.35993 65.39052, -178.90332 65.74044000000001, -178.68611 66.11211, -179.88377 65.87456, -179.43268 65.40411, -180 64.97970870219837)")
b = wkt.loads("LINESTRING (180 64.97432999999999, 178.7072000000003 64.53493, 177.4112800000002 64.60821, 178.3130000000002 64.07593, 178.9082500000002 63.25197000000014, 179.37034 62.98262000000011, 179.48636 62.56894, 179.2282500000001 62.30410000000015, 177.3643 62.5219, 174.5692900000002 61.76915, 173.68013 61.65261, 172.15 60.95, 170.6985000000001 60.33618, 170.3308500000003 59.88177, 168.90046 60.57355, 166.2949800000003 59.78855000000021, 165.8400000000002 60.16, 164.87674 59.7316, 163.5392900000001 59.86871, 163.2171100000002 59.21101, 162.0173300000001 58.24328, 162.05297 57.83912, 163.19191 57.6150300000001, 163.0579400000002 56.15924000000012, 162.1295800000002 56.12219, 161.70146 55.28568000000016, 162.1174900000002 54.85514, 160.3687700000004 54.34433, 160.0217300000002 53.20257, 158.5309400000002 52.95868000000024, 158.23118 51.94269, 156.7897900000003 51.01105, 156.4200000000002 51.7, 155.99182 53.15895, 155.4336600000001 55.38103000000012, 155.9144200000003 56.76792000000015, 156.75815 57.3647, 156.8103500000001 57.83204, 158.3643300000002 58.05575, 160.1506400000002 59.31477000000012, 161.87204 60.34300000000013, 163.66969 61.1409000000001, 164.4735500000001 62.55061, 163.2584200000002 62.46627, 162.65791 61.6425, 160.1214800000001 60.54423, 159.30232 61.7739600000001, 156.7206800000001 61.43442, 154.2180600000003 59.75818000000012, 155.04375 59.14495, 152.81185 58.88385, 151.2657300000003 58.78089, 151.3381500000001 59.50396, 149.78371 59.65573000000015, 148.54481 59.16448, 145.48722 59.33637, 142.1978200000002 59.03998, 138.9584800000003 57.08805, 135.12619 54.72959, 136.70171 54.60355000000013, 137.19342 53.97732, 138.1647 53.75501000000025, 138.80463 54.25455000000011, 139.90151 54.18968000000018, 141.34531 53.08957000000012, 141.37923 52.23877, 140.5974200000002 51.2396700000001, 140.51308 50.04553000000013, 140.0619300000002 48.44671000000017, 138.5547200000002 46.99965, 138.21971 46.30795, 136.86232 45.14350000000019, 135.5153500000002 43.989, 134.8693900000003 43.39821, 133.5368700000003 42.81147, 132.9062700000001 42.7984900000001, 132.2780700000003 43.28456000000011, 130.9358700000002 42.55274, 130.78 42.2200000000002, 130.6400000000002 42.395, 130.6338664084098 42.90301463477056, 131.144687941615 42.92998973242695, 131.2885551291156 44.11151968034827, 131.0251900000003 44.96796, 131.8834542176596 45.32116160743652, 133.0971200000002 45.14409, 133.7696439963132 46.11692698829916, 134.1123500000002 47.21248000000014, 134.50081 47.57845000000015, 135.0263114767868 48.47822988544391, 133.373595819228 48.18344167743484, 132.5066900000001 47.78896, 130.9872600000001 47.79013, 130.5822933289827 48.7296874049762, 129.3978178244205 49.44060008401561, 127.6574000000004 49.76027, 127.2874556824849 50.73979726826545, 126.9391565288379 51.35389415140591, 126.564399041857 51.7842554795327, 125.9463489116465 52.79279857035695, 125.0682112977105 53.16104482686893, 123.57147 53.4588, 122.2457479187931 53.4317259792137, 121.0030847514704 53.25140106873124, 120.1770886577169 52.75388621684121, 120.725789015792 52.51622630473091, 120.7382 51.96411, 120.1820800000002 51.64355, 119.27939 50.58292, 119.2884607280259 50.14288279886196, 117.8792444194265 49.51098338479704, 116.6788008972862 49.8885313991214, 115.4856954285314 49.80517731383475, 114.9621098165504 50.14024730081513, 114.3624564962354 50.24830272073748, 112.8977396993544 49.54356537535699, 111.5812309102867 49.37796824807768, 110.6620105326789 49.13012807880585, 109.4024491719967 49.29296051695769, 108.4751672709513 49.28254771585071, 107.8681758972511 49.79370514586589, 106.8888041524553 50.27429596618029, 105.8865914245869 50.40601919209217, 104.62158 50.27532000000016, 103.6765454447604 50.08996613219514, 102.2558900000001 50.51056000000011, 102.06521 51.2599100000001, 100.8894804219626 51.51685578063842, 99.98173221232358 51.63400625264396, 98.86149051310051 52.04736603454671, 97.82573978067452 51.01099518493325, 98.23176150919173 50.42240062112873, 97.25976000000023 49.72605, 95.81402000000017 49.97746000000012, 94.81594933469879 50.01343333597089, 94.14756635943561 50.48053660745717, 93.10420999999999 50.49529, 92.23471154171969 50.80217072204175, 90.71366743364078 50.3318118353211, 88.80556684769559 49.47052073831247, 87.75126427607685 49.29719798440556, 87.35997033076271 49.21498078062916, 86.82935672398966 49.82667470966814, 85.5412699726825 49.69285858824816, 85.11555952346211 50.11730296487764, 84.41637739455305 50.31139964456582, 83.93511478061893 50.88924551045358, 83.38300377801247 51.0691828476939, 81.94598554883996 50.81219594990634, 80.56844689323546 51.38833649352844, 80.03555952344172 50.86475088154722, 77.80091556184433 53.40441498474755, 76.52517947785478 54.17700348572714, 76.89110029491346 54.49052440044193, 74.38482000000013 53.54685000000012, 73.42567874542053 53.48981028910976, 73.50851606638437 54.0356167669766, 72.22415001820221 54.37665538188679, 71.1801310566095 54.13328522400826, 70.86526655465516 55.16973358827011, 69.06816694527291 55.3852501491435, 68.16910037625891 54.97039175070438, 65.6668700000001 54.60125000000016, 65.17853356309595 54.35422781027208, 61.43660000000014 54.00625, 60.97806644068325 53.66499339457914, 61.69998619980063 52.97999644633427, 60.73999311711455 52.71998647725775, 60.92726850774025 52.44754832621501, 59.96753380721557 51.96042043721567, 61.58800337102414 51.27265879984319, 61.33742435084102 50.79907013610426, 59.93280724471558 50.84219411885184, 59.64228234237058 50.54544220641571, 58.36332000000013 51.06364, 56.77798 51.04355, 55.71694000000011 50.62171000000015, 54.5328784523762 51.02623973245937, 52.32872358583106 51.7186522487381, 50.76664839051219 51.69276235615988, 48.70238162618105 50.60512848571284, 48.57784142435762 49.87475962991564, 47.54948042174939 50.45469839131113, 46.75159630716277 49.35600576435374, 47.0436715024766 49.15203888609759, 46.4664457537763 48.39415233010493, 47.31524000000016 47.71585, 48.05725 47.74377, 48.69473351420189 47.0756281601779, 48.59325000000015 46.56104000000011, 49.10116000000014 46.39933000000011, 48.64541000000011 45.80629, 47.67591 45.64149000000012, 46.68201 44.6092000000001, 47.59094 43.66016000000013, 47.49252 42.98658, 48.58437000000018 41.80888, 47.98728315612604 41.4058192001944, 47.81566572448466 41.15141612402135, 47.37331546406639 41.21973236751114, 46.68607059101672 41.82713715266991, 46.40495079934894 41.86067515722743, 45.7764 42.09244000000024, 45.47027916848592 42.50278066667005, 44.53762291848207 42.71199270280368, 43.93121000000011 42.55496000000011, 43.7559900000002 42.74083, 42.39440000000016 43.2203, 40.92219000000014 43.38215000000014, 40.07696495947985 43.55310415300249, 39.95500857927109 43.43499766699929, 38.68 44.28, 37.53912000000011 44.65721, 36.67546000000013 45.24469, 37.40317 45.4045100000001, 38.23295 46.24087, 37.67372 46.63657, 39.14767 47.04475000000014, 39.12120000000013 47.26336, 38.22353803889948 47.10218984637598, 38.25511233902981 47.54640045835697, 38.77057 47.82562000000024, 39.738277622239 47.89893707945208, 39.89562000000015 48.23241, 39.67465 48.78382000000013, 40.08078901546949 49.30742991799937, 40.06904000000011 49.60105, 38.59498823421356 49.92646190042373, 38.01063113785708 49.91566152607473, 37.39345950699524 50.38395335550368, 36.62616784032539 50.22559092874513, 35.35611616388812 50.57719737405915, 35.37791 50.77394, 35.02218305841794 51.2075723333715, 34.22481570815441 51.25599315042894, 34.14197838719062 51.5664134792062, 34.39173058445724 51.76888174092591, 33.75269982273588 52.33507457133166, 32.71576053236717 52.23846548116217, 32.41205813978777 52.28869497334978, 32.15944000000022 52.06125000000011, 31.78597 52.10168, 31.54001834486226 52.74205231384644, 31.30520063652799 53.07399587667331, 31.49764 53.16743000000014, 32.30451948418838 53.13272614197285, 32.69364301934613 53.35142080343215, 32.40559858575116 53.61804535584201, 31.73127282077459 53.79402944601202, 31.79142418796241 53.9746385768722, 31.38447228366383 54.15705638286238, 30.75753380709878 54.8117709417844, 30.97183597181325 55.08154775656413, 30.87390913262007 55.55097646750352, 29.89629438652244 55.7894632025305, 29.37157189303079 55.67009064393628, 29.22951338066039 55.91834422466641, 28.17670942557794 56.16912995057879, 27.85528201672253 56.75932648378438, 27.77001590344099 57.2442581244112, 27.28818484875165 57.47452830670392, 27.71668582531578 57.79189911562446, 27.42015000000021 58.72457000000014, 28.13169925305186 59.300825100331, 27.98112 59.47537, 29.1177 60.02805000000012, 28.07 60.50352000000015, 30.21110721204465 61.78002777774969, 31.13999108249104 62.35769277612445, 31.51609215671127 62.8676874864129, 30.0358724301428 63.55281362573857, 30.44468468600374 64.20445343693908, 29.54442955904702 64.94867157659056, 30.21765 65.80598000000001, 29.05458865735238 66.94428620062203, 29.9774263852207 67.69829702419275, 28.44594363781877 68.364612942164, 28.59192955904336 69.0647769232867, 29.39955 69.15692000000018, 31.10108000000011 69.55811, 32.13272000000026 69.90595000000025, 33.77547 69.30142000000012, 36.51396 69.06341999999999, 40.29234000000017 67.9324, 41.05987000000013 67.45713000000012, 41.12595000000019 66.79158000000012, 40.01583 66.26618000000013, 38.38295 65.99953000000011, 33.91871000000017 66.75961, 33.18444 66.63253, 34.81477 65.90015000000014, 34.87857425307877 65.43621287704821, 34.94391000000016 64.41437000000016, 36.23129 64.10945, 37.01273000000012 63.84983000000011, 37.14197000000016 64.33471, 36.53957903508982 64.76446, 37.17604000000014 65.14322000000013, 39.59345 64.52079000000018, 40.43560000000011 64.76446, 39.76260000000016 65.49682, 42.0930900000001 66.47623, 43.01604000000012 66.41858000000011, 43.94975000000014 66.06908, 44.53226 66.75634000000014, 43.69839 67.35245, 44.18795000000014 67.95050999999999, 43.45282 68.57079, 46.25000000000014 68.25, 46.82134000000016 67.68997, 45.55517 67.56652, 45.5620200000001 67.01005000000021, 46.34915000000015 66.6676700000001, 47.89416000000026 66.88455000000016, 48.13876 67.52238, 50.22766000000016 67.99867000000015, 53.71743000000018 68.85738000000012, 54.47171 68.80815, 53.48582000000013 68.20131000000001, 54.72628 68.09702, 55.44268000000014 68.43866, 57.31702000000016 68.46628, 58.80200000000022 68.88082, 59.94142000000019 68.2784400000001, 61.07784000000018 68.94069, 60.03 69.52, 60.55 69.84999999999999, 63.50400000000016 69.54738999999999, 64.888115 69.23483500000015, 68.51216000000014 68.09233000000017, 69.18068 68.61563000000012, 68.16444 69.14436000000001, 68.13522 69.35648999999999, 66.93008000000012 69.45461000000012, 67.25976 69.92873, 66.72492000000014 70.70889000000014, 66.69466 71.02897000000024, 68.54006000000012 71.93450000000024, 69.19636000000011 72.84336000000016, 69.94 73.04000000000013, 72.58754 72.7762900000001, 72.79603 72.22006, 71.8481100000001 71.40898, 72.47011000000001 71.09019000000001, 72.79188000000001 70.39113999999999, 72.56470000000022 69.02085, 73.66786999999999 68.4079, 73.23869999999999 67.74039999999999, 71.28000000000011 66.32000000000016, 72.42301000000018 66.17267000000018, 72.82077 66.53267, 73.92099000000016 66.78946000000013, 74.1865100000002 67.28429, 75.05200000000001 67.76047000000017, 74.46926000000016 68.32899, 74.93584000000013 68.98918, 73.84236 69.07146, 73.60187000000022 69.62763, 74.3998 70.63175, 73.1011 71.44717000000026, 74.89082000000022 72.12119, 74.65926 72.83226999999999, 75.15801000000019 72.85497000000012, 75.68351 72.30056000000013, 75.28898000000012 71.33556, 76.35911 71.15287000000015, 75.90313000000017 71.87401, 77.57665000000011 72.26716999999999, 79.65202000000014 72.32011, 81.5 71.75, 80.61071000000013 72.58285000000012, 80.51109 73.6482, 82.25 73.85000000000011, 84.65526 73.80591000000018, 86.82230000000024 73.93688, 86.00955999999999 74.45967000000016, 87.16682000000017 75.11642999999999, 88.31571000000011 75.14393, 90.26000000000001 75.64, 92.90058000000001 75.77333, 93.23421000000016 76.0472, 95.86000000000016 76.1400000000001, 96.67821000000001 75.91548, 98.92254000000023 76.44689, 100.7596700000002 76.43028, 101.03532 76.86189, 101.9908400000001 77.28754000000021, 104.3516000000001 77.69792, 106.0666400000001 77.37389, 104.7050000000002 77.12739999999999, 106.9701300000001 76.97418999999999, 107.2400000000002 76.48, 108.1538 76.72335000000015, 111.0772600000002 76.70999999999999, 113.33151 76.22224, 114.13417 75.84764, 113.88539 75.32779000000014, 112.77918 75.03185999999999, 110.1512500000002 74.47673, 109.4 74.18000000000001, 110.64 74.04000000000001, 112.11919 73.78774000000013, 113.0195400000003 73.97693000000015, 113.5295800000003 73.33505000000011, 113.96881 73.5948800000001, 115.56782 73.75285, 118.7763300000002 73.58772, 119.02 73.12, 123.2006600000001 72.97122, 123.2577700000002 73.73503000000011, 125.3800000000002 73.56, 126.97644 73.56549, 128.59126 73.03870999999999, 129.05157 72.39872, 128.4600000000001 71.98, 129.7159900000002 71.19304, 131.2885800000003 70.78699000000012, 132.2535000000002 71.83630000000011, 133.8576600000003 71.38642000000016, 135.56193 71.65525000000014, 137.49755 71.34763, 138.2340900000002 71.62803, 139.8698300000001 71.48783000000014, 139.14791 72.41619000000011, 140.46817 72.84941000000015, 149.5 72.2, 150.3511800000002 71.60643, 152.9689000000002 70.84222, 157.00688 71.03140999999999, 158.99779 70.86672, 159.8303100000003 70.45323999999999, 159.70866 69.72198, 160.9405300000003 69.4372800000001, 162.2790700000001 69.64203999999999, 164.0524800000001 69.66822999999999, 165.9403700000002 69.47199000000001, 167.83567 69.58269, 169.5776300000002 68.6938, 170.8168800000003 69.01363000000001, 170.0082000000002 69.65276, 170.4534500000003 70.09703, 173.6439100000003 69.81743, 175.7240300000002 69.87725000000023, 178.6 69.40000000000001, 180 68.96363636363657)")
def plot_line(ob, ax, **kwargs):
x, y = ob.xy
ax.plot(x, y, linewidth=1, zorder=1, **kwargs)
def simplify(x, y, tol=8):
return round(x, tol), round(y, tol)
def shift(x, y):
"""Sometimes x (and y) is a tuple, sometimes a float?!
Note: We could also do the opposite of this to move things
back into place...
"""
try:
x = [i + 360 if i < 0 else i for i in x]
except:
x = x + 360 if x < 0 else x
return x, y
fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(10, 4))
# This obviously isn't going to work...
l = ops.linemerge([a, b])
for i in l:
plot_line(i, ax1)
# This should work, but the points are _exactly_ coincident
l = ops.linemerge([ops.transform(shift, a), b])
for i in l:
plot_line(i, ax2)
# This _does_ work, but the process is 'destructive' (i.e., we lose precision)
l = ops.linemerge([ops.transform(simplify, ops.transform(shift, i)) for i in [a, b]])
plot_line(l, ax3)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment