Created
July 11, 2023 22:54
-
-
Save calebrob6/12856358f6a9a82c017e20f45078fb54 to your computer and use it in GitHub Desktop.
Show the calculated distance in meters between three points with the same longitude using different coordinate systems/methods.
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": "code", | |
"execution_count": 38, | |
"id": "3de7bed1", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import shapely.geometry\n", | |
"import fiona.transform\n", | |
"import geopy.distance\n", | |
"\n", | |
"seattle_lon, seattle_lat = -122, 47\n", | |
"san_fransisco_lon, san_fransisco_lat = -122, 38\n", | |
"some_other_lon, some_other_lat = -122, 56" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 39, | |
"id": "cdfbc4ca", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"seattle_geom = shapely.geometry.mapping(shapely.geometry.Point(seattle_lon, seattle_lat))\n", | |
"san_fransisco_geom = shapely.geometry.mapping(shapely.geometry.Point(san_fransisco_lon, san_fransisco_lat))\n", | |
"some_other_geom = shapely.geometry.mapping(shapely.geometry.Point(some_other_lon, some_other_lat))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 40, | |
"id": "74344d0f", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"seattle_utm = shapely.geometry.shape(fiona.transform.transform_geom(\"epsg:4326\", \"EPSG:32610\", seattle_geom))\n", | |
"san_fransisco_utm = shapely.geometry.shape(fiona.transform.transform_geom(\"epsg:4326\", \"EPSG:32610\", san_fransisco_geom))\n", | |
"some_other_utm = shapely.geometry.shape(fiona.transform.transform_geom(\"epsg:4326\", \"EPSG:32610\", some_other_geom))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 41, | |
"id": "06cfe70e", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"seattle_equal_area = shapely.geometry.shape(fiona.transform.transform_geom(\"epsg:4326\", \"EPSG:6933\", seattle_geom))\n", | |
"san_fransisco_equal_area = shapely.geometry.shape(fiona.transform.transform_geom(\"epsg:4326\", \"EPSG:6933\", san_fransisco_geom))\n", | |
"some_other_equal_area = shapely.geometry.shape(fiona.transform.transform_geom(\"epsg:4326\", \"EPSG:6933\", some_other_geom))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 42, | |
"id": "c7751a80", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"seattle_bad = shapely.geometry.shape(fiona.transform.transform_geom(\"epsg:4326\", \"EPSG:3857\", seattle_geom))\n", | |
"san_fransisco_bad = shapely.geometry.shape(fiona.transform.transform_geom(\"epsg:4326\", \"EPSG:3857\", san_fransisco_geom))\n", | |
"some_other_bad = shapely.geometry.shape(fiona.transform.transform_geom(\"epsg:4326\", \"EPSG:3857\", some_other_geom))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 43, | |
"id": "503b0646", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"seattle_azimuthal = shapely.geometry.shape(fiona.transform.transform_geom(\"epsg:4326\", \"ESRI:102016\", seattle_geom))\n", | |
"san_fransisco_azimuthal = shapely.geometry.shape(fiona.transform.transform_geom(\"epsg:4326\", \"ESRI:102016\", san_fransisco_geom))\n", | |
"some_other_azimuthal = shapely.geometry.shape(fiona.transform.transform_geom(\"epsg:4326\", \"ESRI:102016\", some_other_geom))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 44, | |
"id": "625b5f1a", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"(999431.9345002294, 1000974.6564423705)" | |
] | |
}, | |
"execution_count": 44, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"#%%timeit -n 10000 -r 7\n", | |
"seattle_utm.distance(san_fransisco_utm), seattle_utm.distance(some_other_utm)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 45, | |
"id": "3e2ea858", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"(850814.3446692536, 719874.2665405516)" | |
] | |
}, | |
"execution_count": 45, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"#%%timeit -n 10000 -r 7\n", | |
"seattle_equal_area.distance(san_fransisco_equal_area), seattle_equal_area.distance(some_other_equal_area)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 46, | |
"id": "23c38992", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"(1362648.2595610106, 1616341.5836506737)" | |
] | |
}, | |
"execution_count": 46, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"#%%timeit -n 10000 -r 7\n", | |
"seattle_bad.distance(san_fransisco_bad), seattle_bad.distance(some_other_bad) # technically meters but shouldn't be used for distances at all!" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 47, | |
"id": "969e8ad0", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"(999748.9898021035, 1001316.0035028645)" | |
] | |
}, | |
"execution_count": 47, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"#%%timeit -n 10000 -r 7\n", | |
"seattle_azimuthal.distance(san_fransisco_azimuthal), seattle_azimuthal.distance(some_other_azimuthal) # https://epsg.io/102016" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 48, | |
"id": "21e31fd5", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"(999748.9898013712, 1001316.003501355)" | |
] | |
}, | |
"execution_count": 48, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"#%%timeit\n", | |
"geopy.distance.distance((seattle_lat, seattle_lon), (san_fransisco_lat, san_fransisco_lon)).m, geopy.distance.distance((seattle_lat, seattle_lon), (some_other_lat, some_other_lon)).m" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 49, | |
"id": "c8f5561d", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"(1000755.7535177224, 1000755.7535177228)" | |
] | |
}, | |
"execution_count": 49, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"#%%timeit\n", | |
"geopy.distance.great_circle((seattle_lat, seattle_lon), (san_fransisco_lat, san_fransisco_lon)).m, geopy.distance.great_circle((seattle_lat, seattle_lon), (some_other_lat, some_other_lon)).m" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "4baa0887", | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "geospatiallib", | |
"language": "python", | |
"name": "geospatiallib" | |
}, | |
"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.9" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 5 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment