trap/test_path_transforms.ipynb

435 lines
30 KiB
Text

{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "6f6fea74",
"metadata": {},
"outputs": [],
"source": [
"import shapely\n",
"from shapely import LineString\n"
]
},
{
"cell_type": "markdown",
"id": "beb9b257",
"metadata": {},
"source": []
},
{
"cell_type": "code",
"execution_count": 2,
"id": "7b3cdaa2",
"metadata": {},
"outputs": [],
"source": [
"line1 = LineString([(0, 0), (2, 2)])\n",
"line2 = LineString([(1, 0), (1, 2)])\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "58df4b55",
"metadata": {},
"outputs": [],
"source": [
"# shapely.intersection(line1, line2)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "f62e013a",
"metadata": {},
"outputs": [],
"source": [
"gap = shapely.intersection(line1.buffer(.1), line2)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "acbd14e8",
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"100.0\" height=\"100.0\" viewBox=\"0.92 -0.08 0.16000000000000003 2.16\" preserveAspectRatio=\"xMinYMin meet\"><g transform=\"matrix(1,0,0,-1,0,2.0)\"><g><polyline fill=\"none\" stroke=\"#66cc99\" stroke-width=\"0.0432\" points=\"1.0,0.0 1.0,0.8585786437626906\" opacity=\"0.8\" /><polyline fill=\"none\" stroke=\"#66cc99\" stroke-width=\"0.0432\" points=\"1.0,1.1414213562373094 1.0,2.0\" opacity=\"0.8\" /></g></g></svg>"
],
"text/plain": [
"<MULTILINESTRING ((1 0, 1 0.859), (1 1.141, 1 2))>"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"gapped = line2.difference(gap)\n",
"gapped"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "716105d1",
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"100.0\" height=\"100.0\" viewBox=\"-0.08 -0.08 2.16 2.16\" preserveAspectRatio=\"xMinYMin meet\"><g transform=\"matrix(1,0,0,-1,0,2.0)\"><g><polyline fill=\"none\" stroke=\"#66cc99\" stroke-width=\"0.0432\" points=\"0.0,0.0 2.0,2.0\" opacity=\"0.8\" /><polyline fill=\"none\" stroke=\"#66cc99\" stroke-width=\"0.0432\" points=\"1.0,0.0 1.0,0.8585786437626906\" opacity=\"0.8\" /><polyline fill=\"none\" stroke=\"#66cc99\" stroke-width=\"0.0432\" points=\"1.0,1.1414213562373094 1.0,2.0\" opacity=\"0.8\" /></g></g></svg>"
],
"text/plain": [
"<MULTILINESTRING ((0 0, 2 2), (1 0, 1 0.859), (1 1.141, 1 2))>"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"line1.union(gapped)"
]
},
{
"cell_type": "code",
"execution_count": 77,
"id": "366565dc",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 77,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import pickle\n",
"with open('/home/ruben/tmp/hof3-demo-renderablelines.pcl', 'rb') as fp:\n",
" rl = pickle.load(fp)\n",
"len(rl.lines)"
]
},
{
"cell_type": "code",
"execution_count": 82,
"id": "e97ccc33",
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"100.0\" height=\"100.0\" viewBox=\"12.574825275691582 1.1233588345371024 12.94088203081458 12.86814393847737\" preserveAspectRatio=\"xMinYMin meet\"><g transform=\"matrix(1,0,0,-1,0,15.114861607551575)\"><g><polyline fill=\"none\" stroke=\"#66cc99\" stroke-width=\"0.2588176406162916\" points=\"13.054117202758789,1.602650761604309 13.236830711364746,2.323932647705078 13.263605117797852,2.9978199005126953 13.254789352416992,3.509000301361084 13.26492691040039,4.013772487640381 13.2764892578125,4.605075359344482 13.275935173657013,4.688779627063853\" opacity=\"0.8\" /><polyline fill=\"none\" stroke=\"#66cc99\" stroke-width=\"0.2588176406162916\" points=\"13.275935173657013,4.688779627063853 13.272747039794922,5.170403957366943 13.277579307556152,5.716872215270996 13.284239768981934,6.300223350524902 13.294684410095215,6.80850887298584 13.338418006896973,7.353998184204102 13.453015327453613,7.930163383483887 13.473861806028852,8.001998688301857\" opacity=\"0.8\" /><polyline fill=\"none\" stroke=\"#66cc99\" stroke-width=\"0.2588176406162916\" points=\"13.473861806028852,8.001998688301857 13.59854507446289,8.431647300720215 13.79714584350586,8.925849914550781 14.103094100952148,9.44881820678711 14.468462944030762,9.825448989868164 14.813995361328125,10.043636322021484 15.20891284942627,10.21924114227295 15.714356422424316,10.3283109664917 16.250303268432617,10.384780883789062 16.6823787689209,10.437690734863281 16.991291046142578,10.4764986038208 17.331056594848633,10.502613067626953 17.73456573486328,10.512210845947266 18.004163068674686,10.492859999933291\" opacity=\"0.8\" /><polyline fill=\"none\" stroke=\"#66cc99\" stroke-width=\"0.2588176406162916\" points=\"18.004163068674686,10.492859999933291 18.128488540649414,10.483936309814453 18.487953186035156,10.452094078063965 18.785362243652344,10.436294555664062\" opacity=\"0.8\" /><polyline fill=\"none\" stroke=\"#66cc99\" stroke-width=\"0.2588176406162916\" points=\"13.85411720275879,1.602650761604309 14.036830711364747,2.323932647705078 14.063605117797852,2.9978199005126953 14.054789352416993,3.509000301361084 14.064926910400391,4.013772487640381 14.0764892578125,4.605075359344482 14.072747039794923,5.170403957366943 14.077579307556153,5.716872215270996 14.084239768981934,6.300223350524902 14.094684410095216,6.80850887298584 14.138418006896973,7.353998184204102 14.253015327453614,7.930163383483887 14.398545074462891,8.431647300720215 14.59714584350586,8.925849914550781 14.90309410095215,9.44881820678711 15.268462944030762,9.825448989868164 15.613995361328126,10.043636322021484 16.00891284942627,10.21924114227295 16.514356422424317,10.3283109664917 17.050303268432618,10.384780883789062 17.4823787689209,10.437690734863281 17.79129104614258,10.4764986038208 18.004163068674686,10.492859999933291\" opacity=\"0.8\" /><polyline fill=\"none\" stroke=\"#66cc99\" stroke-width=\"0.2588176406162916\" points=\"18.004163068674686,10.492859999933291 18.131056594848634,10.502613067626953 18.534565734863282,10.512210845947266 18.928488540649415,10.483936309814453 19.287953186035157,10.452094078063965 19.559287652739457,10.43767973904246\" opacity=\"0.8\" /><polyline fill=\"none\" stroke=\"#66cc99\" stroke-width=\"0.2588176406162916\" points=\"19.559287652739457,10.43767973904246 19.585362243652344,10.436294555664062\" opacity=\"0.8\" /><polyline fill=\"none\" stroke=\"#66cc99\" stroke-width=\"0.2588176406162916\" points=\"13.254117202758788,4.602650761604309 13.275935173657013,4.688779627063853\" opacity=\"0.8\" /><polyline fill=\"none\" stroke=\"#66cc99\" stroke-width=\"0.2588176406162916\" points=\"13.275935173657013,4.688779627063853 13.436830711364745,5.323932647705078 13.46360511779785,5.997819900512695 13.454789352416991,6.509000301361084 13.46492691040039,7.013772487640381 13.4764892578125,7.605075359344482 13.473861806028852,8.001998688301857\" opacity=\"0.8\" /><polyline fill=\"none\" stroke=\"#66cc99\" stroke-width=\"0.2588176406162916\" points=\"13.473861806028852,8.001998688301857 13.472747039794921,8.170403957366943 13.477579307556152,8.716872215270996 13.484239768981933,9.300223350524902 13.494684410095214,9.80850887298584 13.538418006896972,10.353998184204102 13.653015327453613,10.930163383483887 13.79854507446289,11.431647300720215 13.997145843505859,11.925849914550781 14.303094100952148,12.44881820678711 14.668462944030761,12.825448989868164 15.013995361328124,13.043636322021484 15.408912849426269,13.21924114227295 15.914356422424316,13.3283109664917 16.450303268432616,13.384780883789062 16.882378768920898,13.437690734863281 17.191291046142577,13.4764986038208 17.531056594848632,13.502613067626953 17.93456573486328,13.512210845947266 18.328488540649413,13.483936309814453 18.687953186035156,13.452094078063965 18.985362243652343,13.436294555664062\" opacity=\"0.8\" /><polyline fill=\"none\" stroke=\"#66cc99\" stroke-width=\"0.2588176406162916\" points=\"19.30235349847065,10.633026873519269 19.235657316138603,10.72162989441483 19.25142314708296,10.724591937990125 19.276052686417287,10.723307625855627 19.305675383176524,10.720456648437072 19.34152995310335,10.713420416622892 19.38405990992679,10.692576591414149 19.447898276106976,10.627548119769324 19.538789815688528,10.469533168868804 19.559287652739457,10.43767973904246\" opacity=\"0.8\" /><polyline fill=\"none\" stroke=\"#66cc99\" stroke-width=\"0.2588176406162916\" points=\"19.559287652739457,10.43767973904246 19.618562645229282,10.34556700495055 19.70095445074339,10.276282688478606 19.78572222082481,10.226648518182856 19.87096394005848,10.18523869132758 19.95578926646726,10.147993633193526 20.039809809669062,10.112997353024198 20.12278153642564,10.079136848981925 20.204486664928552,10.045732289793875 20.284743389119782,10.01239580999622 20.363438810367295,9.978933880375678 20.440539327689788,9.945281693889882 20.516070865773006,9.911451477448043 20.59010815536169,9.877505775099484 20.66274901144671,9.8435308505156 20.734107030808293,9.809616678682588 20.804285833396992,9.77583834599902 20.873371902198635,9.742248109457197 20.941426154718293,9.708885599000865 21.00848064165915,9.67579245274926 21.074544736159016,9.643031397408489 21.139620381939004,9.610691741984184 21.2037087026919,9.578888480289564 21.266817634745795,9.547744299332871 21.32896640096682,9.517380646960357 21.390188610409957,9.487905553244364 21.450528602152776,9.459408522827909 21.510045076763788,9.431957371112443 21.568808564521284,9.405602456790954 21.62689632102788,9.380375686941449 21.68439390684711,9.35629710654519 21.741386703697746,9.333373569497342 21.797957410784708,9.311608516090157 21.85417848709533,9.290996631784255 21.910108633041794,9.271532039732094 21.965777034033106,9.253203848596002 22.021186412693726,9.235995497201435 22.076305451889368,9.219877701209256 22.13108487645822,9.204794718150218 22.185471542144644,9.190670250347127 22.239417733721936,9.177433218675239 22.292883815713285,9.165055051057452 22.34583983068329,9.153577908923227 22.398285222311063,9.143143992745157 22.450242704326897,9.133982824531133 22.501766347156916,9.126409895738254 22.55293916188466,9.120838829957465 22.603835691107577,9.117661877167421 22.654563338935304,9.11710285170208 22.705249402698374,9.118968815558663 22.75578383615076,9.122947077013364 22.80568364743966,9.128957573751189 22.854583904951934,9.137023755837419 22.90236928798291,9.147074376145506 22.94904987863285,9.158993959557673 22.99468087633113,9.172673609702402 23.03934310126688,9.188025948211523 23.08316101744814,9.204980192943761 23.126310482841003,9.223470711436422 23.16895876826438,9.243408732188394 23.21123062548745,9.264650997773725 23.253194838686667,9.28697558393772 23.294861303169853,9.31011848946332 23.33618871586419,9.333834241506434 23.377118494175676,9.357928594716116 23.41763425102119,9.38224694999395 23.45779107595487,9.406646020652888 23.497678099638506,9.430992417664314 23.53735396027813,9.455166133051392 23.576829728660943,9.479055497292427 23.616109042722336,9.502560444468756 23.65520348459794,9.525595000536052 23.694141784310343,9.548093202364463 23.73297274913239,9.570012710588799 23.771750575523942,9.59132563302614 23.810531449691645,9.612017761083328 23.849359369123103,9.632083638813189 23.88825639645363,9.651531055786645 23.927233881199683,9.670396162499387 23.966275574191442,9.688733997146388 24.00534448386035,9.706607298607532 24.044380207673687,9.724087328009508 24.083305120393476,9.741235169546215 24.122039004745247,9.758103977122468 24.160501654077024,9.774732105579885 24.198630460261732,9.791151807751016 24.236376785155485,9.807378240225678 24.27370631172517,9.823423719594865 24.310598028361667,9.839291979723498 24.34703637983491,9.854983697103918 24.383015390654716,9.870501783783116 24.418531533142456,9.885844228400362 24.453583917274187,9.901013522163536 24.488173524189662,9.916005471061661 24.522303738085007,9.930820848253884 24.55597713895513,9.945457457193635 24.589196208753805,9.95991515849361 24.62196707001563,9.974193186733359 24.6542923839152,9.988283373942723 24.686176591563445,10.002186514868521 24.71762608909014,10.015899051707237 24.7486434896471,10.029414091779724 24.779236099205903,10.0427325693195 24.809409819776832,10.055846498080005 24.839167935092696,10.068756951916328 24.868518852167572,10.081460379607423 24.89746579564506,10.093951746384134 24.926017576893315,10.106233494526082 24.954180666202706,10.118302479966317 24.981961582649543,10.130157671885225 25.00937066082044,10.14180138289813 25.036415379438957,10.153233485711318\" opacity=\"0.8\" /></g></g></svg>"
],
"text/plain": [
"<MULTILINESTRING ((13.054 1.603, 13.237 2.324, 13.264 2.998, 13.255 3.509, 1...>"
]
},
"execution_count": 82,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"line1 = LineString([p.position for p in rl.lines[0].points])\n",
"line1_similar = LineString([[p.position[0]+.8, p.position[1]] for p in rl.lines[0].points])\n",
"line1_similar2 = LineString([[p.position[0]+.2, p.position[1]+3] for p in rl.lines[0].points])\n",
"\n",
"line2 = LineString([p.position for p in rl.lines[1].points])\n",
"\n",
"all_lines = [line1, line1_similar, line1_similar2, line2]\n",
"\n",
"shapely.union_all(all_lines)\n"
]
},
{
"cell_type": "code",
"execution_count": 81,
"id": "694050df",
"metadata": {},
"outputs": [],
"source": [
"from typing import List\n",
"\n",
"\n",
"def remove_overlaps(line_strings: List[LineString], boundary=.3):\n",
" resulting_geometries: List[shapely.BaseGeometry] = []\n",
" for line in line_strings:\n",
" if not len(resulting_geometries):\n",
" resulting_geometries.append(line)\n",
" continue\n",
"\n",
" current_shape = shapely.union_all(resulting_geometries)\n",
" result = line.difference(current_shape.buffer(boundary))\n",
" # if result.is_empty: # keep empty, so that we keep track of index of the lines\n",
" # continue\n",
" resulting_geometries.append(result)\n",
" return resulting_geometries\n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0cf807fe",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"4 line\n"
]
},
{
"data": {
"image/svg+xml": [
"<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"100.0\" height=\"100.0\" viewBox=\"12.574825275691582 1.1233588345371024 12.94088203081458 12.86814393847737\" preserveAspectRatio=\"xMinYMin meet\"><g transform=\"matrix(1,0,0,-1,0,15.114861607551575)\"><g><polyline fill=\"none\" stroke=\"#66cc99\" stroke-width=\"0.2588176406162916\" points=\"13.054117202758789,1.602650761604309 13.236830711364746,2.323932647705078 13.263605117797852,2.9978199005126953 13.254789352416992,3.509000301361084 13.26492691040039,4.013772487640381 13.2764892578125,4.605075359344482 13.272747039794922,5.170403957366943 13.277579307556152,5.716872215270996 13.284239768981934,6.300223350524902 13.294684410095215,6.80850887298584 13.338418006896973,7.353998184204102 13.453015327453613,7.930163383483887 13.59854507446289,8.431647300720215 13.79714584350586,8.925849914550781 14.103094100952148,9.44881820678711 14.468462944030762,9.825448989868164 14.813995361328125,10.043636322021484 15.20891284942627,10.21924114227295 15.714356422424316,10.3283109664917 16.250303268432617,10.384780883789062 16.6823787689209,10.437690734863281 16.991291046142578,10.4764986038208 17.331056594848633,10.502613067626953 17.73456573486328,10.512210845947266 18.128488540649414,10.483936309814453 18.487953186035156,10.452094078063965 18.785362243652344,10.436294555664062\" opacity=\"0.8\" /><polyline fill=\"none\" stroke=\"#66cc99\" stroke-width=\"0.2588176406162916\" points=\"13.85411720275879,1.602650761604309 14.036830711364747,2.323932647705078 14.063605117797852,2.9978199005126953 14.054789352416993,3.509000301361084 14.064926910400391,4.013772487640381 14.0764892578125,4.605075359344482 14.072747039794923,5.170403957366943 14.077579307556153,5.716872215270996 14.084239768981934,6.300223350524902 14.094684410095216,6.80850887298584 14.138418006896973,7.353998184204102 14.253015327453614,7.930163383483887 14.398545074462891,8.431647300720215 14.59714584350586,8.925849914550781 14.90309410095215,9.44881820678711 15.268462944030762,9.825448989868164 15.508410492120145,9.97696448524149\" opacity=\"0.8\" /><polyline fill=\"none\" stroke=\"#66cc99\" stroke-width=\"0.2588176406162916\" points=\"19.083128366329106,10.470237946900728 19.287953186035157,10.452094078063965 19.585362243652344,10.436294555664062\" opacity=\"0.8\" /><polyline fill=\"none\" stroke=\"#66cc99\" stroke-width=\"0.2588176406162916\" points=\"13.480144794257011,8.941568296181002 13.484239768981933,9.300223350524902 13.494684410095214,9.80850887298584 13.538418006896972,10.353998184204102 13.653015327453613,10.930163383483887 13.79854507446289,11.431647300720215 13.997145843505859,11.925849914550781 14.303094100952148,12.44881820678711 14.668462944030761,12.825448989868164 15.013995361328124,13.043636322021484 15.408912849426269,13.21924114227295 15.914356422424316,13.3283109664917 16.450303268432616,13.384780883789062 16.882378768920898,13.437690734863281 17.191291046142577,13.4764986038208 17.531056594848632,13.502613067626953 17.93456573486328,13.512210845947266 18.328488540649413,13.483936309814453 18.687953186035156,13.452094078063965 18.985362243652343,13.436294555664062\" opacity=\"0.8\" /><polyline fill=\"none\" stroke=\"#66cc99\" stroke-width=\"0.2588176406162916\" points=\"19.794964627824562,10.222158621955673 19.87096394005848,10.18523869132758 19.95578926646726,10.147993633193526 20.039809809669062,10.112997353024198 20.12278153642564,10.079136848981925 20.204486664928552,10.045732289793875 20.284743389119782,10.01239580999622 20.363438810367295,9.978933880375678 20.440539327689788,9.945281693889882 20.516070865773006,9.911451477448043 20.59010815536169,9.877505775099484 20.66274901144671,9.8435308505156 20.734107030808293,9.809616678682588 20.804285833396992,9.77583834599902 20.873371902198635,9.742248109457197 20.941426154718293,9.708885599000865 21.00848064165915,9.67579245274926 21.074544736159016,9.643031397408489 21.139620381939004,9.610691741984184 21.2037087026919,9.578888480289564 21.266817634745795,9.547744299332871 21.32896640096682,9.517380646960357 21.390188610409957,9.487905553244364 21.450528602152776,9.459408522827909 21.510045076763788,9.431957371112443 21.568808564521284,9.405602456790954 21.62689632102788,9.380375686941449 21.68439390684711,9.35629710654519 21.741386703697746,9.333373569497342 21.797957410784708,9.311608516090157 21.85417848709533,9.290996631784255 21.910108633041794,9.271532039732094 21.965777034033106,9.253203848596002 22.021186412693726,9.235995497201435 22.076305451889368,9.219877701209256 22.13108487645822,9.204794718150218 22.185471542144644,9.190670250347127 22.239417733721936,9.177433218675239 22.292883815713285,9.165055051057452 22.34583983068329,9.153577908923227 22.398285222311063,9.143143992745157 22.450242704326897,9.133982824531133 22.501766347156916,9.126409895738254 22.55293916188466,9.120838829957465 22.603835691107577,9.117661877167421 22.654563338935304,9.11710285170208 22.705249402698374,9.118968815558663 22.75578383615076,9.122947077013364 22.80568364743966,9.128957573751189 22.854583904951934,9.137023755837419 22.90236928798291,9.147074376145506 22.94904987863285,9.158993959557673 22.99468087633113,9.172673609702402 23.03934310126688,9.188025948211523 23.08316101744814,9.204980192943761 23.126310482841003,9.223470711436422 23.16895876826438,9.243408732188394 23.21123062548745,9.264650997773725 23.253194838686667,9.28697558393772 23.294861303169853,9.31011848946332 23.33618871586419,9.333834241506434 23.377118494175676,9.357928594716116 23.41763425102119,9.38224694999395 23.45779107595487,9.406646020652888 23.497678099638506,9.430992417664314 23.53735396027813,9.455166133051392 23.576829728660943,9.479055497292427 23.616109042722336,9.502560444468756 23.65520348459794,9.525595000536052 23.694141784310343,9.548093202364463 23.73297274913239,9.570012710588799 23.771750575523942,9.59132563302614 23.810531449691645,9.612017761083328 23.849359369123103,9.632083638813189 23.88825639645363,9.651531055786645 23.927233881199683,9.670396162499387 23.966275574191442,9.688733997146388 24.00534448386035,9.706607298607532 24.044380207673687,9.724087328009508 24.083305120393476,9.741235169546215 24.122039004745247,9.758103977122468 24.160501654077024,9.774732105579885 24.198630460261732,9.791151807751016 24.236376785155485,9.807378240225678 24.27370631172517,9.823423719594865 24.310598028361667,9.839291979723498 24.34703637983491,9.854983697103918 24.383015390654716,9.870501783783116 24.418531533142456,9.885844228400362 24.453583917274187,9.901013522163536 24.488173524189662,9.916005471061661 24.522303738085007,9.930820848253884 24.55597713895513,9.945457457193635 24.589196208753805,9.95991515849361 24.62196707001563,9.974193186733359 24.6542923839152,9.988283373942723 24.686176591563445,10.002186514868521 24.71762608909014,10.015899051707237 24.7486434896471,10.029414091779724 24.779236099205903,10.0427325693195 24.809409819776832,10.055846498080005 24.839167935092696,10.068756951916328 24.868518852167572,10.081460379607423 24.89746579564506,10.093951746384134 24.926017576893315,10.106233494526082 24.954180666202706,10.118302479966317 24.981961582649543,10.130157671885225 25.00937066082044,10.14180138289813 25.036415379438957,10.153233485711318\" opacity=\"0.8\" /></g></g></svg>"
],
"text/plain": [
"<MULTILINESTRING ((13.054 1.603, 13.237 2.324, 13.264 2.998, 13.255 3.509, 1...>"
]
},
"execution_count": 83,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"clean_lines = remove_overlaps(all_lines, .3)\n",
"print(len(clean_lines), \"line\")\n",
"shapely.union_all(clean_lines)"
]
},
{
"cell_type": "code",
"execution_count": 99,
"id": "3b8fb8c9",
"metadata": {},
"outputs": [],
"source": [
"# line3d_1 = LineString([[p.position[0], p.position[1], p.color.alpha] for p in rl.lines[0].points])\n",
"# line3d_2 = LineString([[p.position[0]+1, p.position[1], p.color.alpha] for p in rl.lines[0].points])\n",
"# shapely.union_all([line3d_1, line3d_2])\n",
"# shapely.difference(line3d_1, line3d_2.buffer(.3), axis=0) # axis does not exist"
]
},
{
"cell_type": "markdown",
"id": "198ad339",
"metadata": {},
"source": [
"# Self intersection\n",
"This works different from intersection with another line. Thus, we would need to calculate this"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "8bdeb8e4",
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"100.0\" height=\"100.0\" viewBox=\"-0.12 -0.12 3.24 2.24\" preserveAspectRatio=\"xMinYMin meet\"><g transform=\"matrix(1,0,0,-1,0,2.0)\"><polyline fill=\"none\" stroke=\"#66cc99\" stroke-width=\"0.06480000000000001\" points=\"0.0,0.0 2.0,2.0 3.0,1.0 1.5,0.0 0.0,2.0 3.0,0.0\" opacity=\"0.8\" /></g></svg>"
],
"text/plain": [
"<LINESTRING (0 0, 2 2, 3 1, 1.5 0, 0 2, 3 0)>"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"self_intersecting_line = LineString([[0,0], [2,2], [3,1], [1.5,0], [0,2], [3,0]])\n",
"self_intersecting_line"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "2f47a911",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(True, False)"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"line1.is_simple, self_intersecting_line.is_simple"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "8298eb6a",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"That trick does not work: Input geometry segment overlaps with the splitter.\n"
]
}
],
"source": [
"try:\n",
" diff_line = shapely.ops.split(self_intersecting_line, self_intersecting_line)\n",
"except Exception as e:\n",
" print(\"That trick does not work:\", e)"
]
},
{
"cell_type": "code",
"execution_count": 75,
"id": "9fce60ac",
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"100.0\" height=\"100.0\" viewBox=\"-0.12 -0.12 3.24 2.24\" preserveAspectRatio=\"xMinYMin meet\"><g transform=\"matrix(1,0,0,-1,0,2.0)\"><g><polyline fill=\"none\" stroke=\"#66cc99\" stroke-width=\"0.06480000000000001\" points=\"0.0,0.0 2.0,2.0\" opacity=\"0.8\" /><polyline fill=\"none\" stroke=\"#66cc99\" stroke-width=\"0.06480000000000001\" points=\"2.0,2.0 3.0,1.0\" opacity=\"0.8\" /><polyline fill=\"none\" stroke=\"#66cc99\" stroke-width=\"0.06480000000000001\" points=\"3.0,1.0 1.5,0.0\" opacity=\"0.8\" /><polyline fill=\"none\" stroke=\"#66cc99\" stroke-width=\"0.06480000000000001\" points=\"1.5,0.0 1.0389703151622551,0.6147062464503266\" opacity=\"0.8\" /><polyline fill=\"none\" stroke=\"#66cc99\" stroke-width=\"0.06480000000000001\" points=\"0.6753153991234592,1.0995794678353876 0.0,2.0\" opacity=\"0.8\" /><polyline fill=\"none\" stroke=\"#66cc99\" stroke-width=\"0.06480000000000001\" points=\"0.0,2.0 0.9454415587728429,1.3697056274847714\" opacity=\"0.8\" /><polyline fill=\"none\" stroke=\"#66cc99\" stroke-width=\"0.06480000000000001\" points=\"1.454558441227157,1.0302943725152285 1.9795836543402008,0.6802775637731994\" opacity=\"0.8\" /><polyline fill=\"none\" stroke=\"#66cc99\" stroke-width=\"0.06480000000000001\" points=\"2.520416345659799,0.3197224362268006 3.0,0.0\" opacity=\"0.8\" /></g></g></svg>"
],
"text/plain": [
"<MULTILINESTRING ((0 0, 2 2), (2 2, 3 1), (3 1, 1.5 0), (1.5 0, 1.039 0.615)...>"
]
},
"execution_count": 75,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from shapely import MultiLineString, is_empty, is_simple\n",
"\n",
"def linestring_to_segments(ls: LineString) -> List[LineString]:\n",
" return [\n",
" LineString([self_intersecting_line.coords[i], self_intersecting_line.coords[i+1]]) \n",
" for i in range(len(self_intersecting_line.coords)-1)\n",
" ]\n",
"\n",
"def split_line_at_self_intersection(linestring: LineString, boundary = .3):\n",
" if linestring.is_simple:\n",
" return linestring\n",
" segments = linestring_to_segments(linestring)\n",
" new_segments = split_at_self_intersection(segments, boundary_size=boundary)\n",
" if len(new_segments) == 1:\n",
" return new_segments[0]\n",
" return MultiLineString(new_segments) \n",
"\n",
"\n",
"def split_at_self_intersection(segments, new_segments = [], boundary_size = .3):\n",
" if not len(segments):\n",
" return new_segments\n",
" \n",
" segment = segments.pop(0)\n",
" for ns in new_segments[:-1]: # assume there's no overlap with last one\n",
" if segment.intersects(ns):\n",
" #cut\n",
" parts = segment.difference(ns.buffer(boundary_size))\n",
" if type(parts) is LineString:\n",
" if not parts.is_empty:\n",
" new_segments.append(parts)\n",
" remaining_segments = segments\n",
" elif type(parts) is MultiLineString:\n",
" \n",
" new_segments.append(parts.geoms[0]) # add the first part\n",
" # prepend remaining bit, and calculate from there\n",
" remaining_segments = [parts.geoms[1]]\n",
" remaining_segments.extend(segments)\n",
"\n",
" return split_at_self_intersection(remaining_segments, new_segments, boundary_size)\n",
" pass\n",
" \n",
" new_segments.append(segment)\n",
" return split_at_self_intersection(segments, new_segments, boundary_size)\n",
"\n",
"# MultiLineString(split_at_self_intersection(segments, boundary_size=.1))\n",
"split_line_at_self_intersection(self_intersecting_line, .3)"
]
},
{
"cell_type": "markdown",
"id": "3fe70cb7",
"metadata": {},
"source": [
"# Simplification"
]
},
{
"cell_type": "code",
"execution_count": 61,
"id": "0e82f549",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"27 6\n"
]
},
{
"data": {
"image/svg+xml": [
"<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"100.0\" height=\"100.0\" viewBox=\"12.700771450996399 1.249305009841919 6.437936544418335 9.540335297584534\" preserveAspectRatio=\"xMinYMin meet\"><g transform=\"matrix(1,0,0,-1,0,12.038945317268372)\"><polyline fill=\"none\" stroke=\"#66cc99\" stroke-width=\"0.19080670595169066\" points=\"13.054117202758789,1.602650761604309 13.338418006896973,7.353998184204102 14.103094100952148,9.44881820678711 14.813995361328125,10.043636322021484 15.714356422424316,10.3283109664917 18.785362243652344,10.436294555664062\" opacity=\"0.8\" /></g></svg>"
],
"text/plain": [
"<LINESTRING (13.054 1.603, 13.338 7.354, 14.103 9.449, 14.814 10.044, 15.714...>"
]
},
"execution_count": 61,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print(line1.coords.__len__(), line1.simplify(.15).coords.__len__())\n",
"line1.simplify(.15)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.4"
}
},
"nbformat": 4,
"nbformat_minor": 5
}