Created
November 9, 2021 07:07
-
-
Save michaeldorman/84d5cdaa856764f065795c99e13a2a63 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"id": "6ae74352-eeca-4792-a7f3-3a068eccc87c", | |
"metadata": {}, | |
"source": [ | |
"# Distance between nearest points along line" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import shapely.ops\n", | |
"import shapely.wkt as wkt" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "79a88b2b-783a-4b5a-8ea8-b7307ea5c7f5", | |
"metadata": {}, | |
"source": [ | |
"## Sample data" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"id": "58a7b769", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"line = wkt.loads(\"LINESTRING (2 0.5, 1 1, -1 0, 1 0)\")\n", | |
"pnt1 = wkt.loads(\"POINT (1.8 1)\")\n", | |
"pnt2 = wkt.loads(\"POINT (0.1 0.1)\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"id": "e7d48176", | |
"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=\"-1.12 -0.12 3.24 1.2400000000000002\" preserveAspectRatio=\"xMinYMin meet\"><g transform=\"matrix(1,0,0,-1,0,1.0)\"><g><circle cx=\"0.1\" cy=\"0.1\" r=\"0.09720000000000001\" stroke=\"#555555\" stroke-width=\"0.032400000000000005\" fill=\"#66cc99\" opacity=\"0.6\" /><circle cx=\"1.8\" cy=\"1.0\" r=\"0.09720000000000001\" stroke=\"#555555\" stroke-width=\"0.032400000000000005\" fill=\"#66cc99\" opacity=\"0.6\" /><polyline fill=\"none\" stroke=\"#66cc99\" stroke-width=\"0.06480000000000001\" points=\"2.0,0.5 1.0,1.0 -1.0,0.0 1.0,0.0\" opacity=\"0.8\" /></g></g></svg>", | |
"text/plain": [ | |
"<shapely.geometry.collection.GeometryCollection at 0x7ff5385cbd00>" | |
] | |
}, | |
"execution_count": 8, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"line.union(pnt1).union(pnt2)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "308e0576-8686-4559-87e6-f18344882f6b", | |
"metadata": {}, | |
"source": [ | |
"## Caculate sub-LineString" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"id": "1775f696-51a8-4f6d-bb0a-30706c82809b", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"dist1 = line.project(pnt1)\n", | |
"dist2 = line.project(pnt2)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"id": "c54db20e", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"line2 = shapely.ops.substring(line, dist1, dist2)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 11, | |
"id": "e7d48176", | |
"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=\"-1.112 -0.11199999999999999 3.024 1.2240000000000002\" preserveAspectRatio=\"xMinYMin meet\"><g transform=\"matrix(1,0,0,-1,0,1.0)\"><g><circle cx=\"0.1\" cy=\"0.1\" r=\"0.09072\" stroke=\"#555555\" stroke-width=\"0.03024\" fill=\"#66cc99\" opacity=\"0.6\" /><circle cx=\"1.8\" cy=\"1.0\" r=\"0.09072\" stroke=\"#555555\" stroke-width=\"0.03024\" fill=\"#66cc99\" opacity=\"0.6\" /><polyline fill=\"none\" stroke=\"#66cc99\" stroke-width=\"0.06048\" points=\"1.6400000000000001,0.6799999999999999 1.0,1.0 -1.0,0.0 0.09999999999999964,0.0\" opacity=\"0.8\" /></g></g></svg>", | |
"text/plain": [ | |
"<shapely.geometry.collection.GeometryCollection at 0x7ff4dd25b490>" | |
] | |
}, | |
"execution_count": 11, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"line2.union(pnt1).union(pnt2)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "a2d301ad-e028-49c3-ba17-8de34fca4e5e", | |
"metadata": {}, | |
"source": [ | |
"## Calculate length" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 12, | |
"id": "a754445a-ba4b-40ef-a2b3-24fdc5bc1969", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"4.051609730299722" | |
] | |
}, | |
"execution_count": 12, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"line2.length" | |
] | |
} | |
], | |
"metadata": { | |
"interpreter": { | |
"hash": "767d51c1340bd893661ea55ea3124f6de3c7a262a8b4abca0554b478b1e2ff90" | |
}, | |
"kernelspec": { | |
"display_name": "Python 3.8.10 64-bit", | |
"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.8.10" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 5 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment