Last active
March 28, 2019 20:38
-
-
Save galak75/21b11734a3b618d05645152ee0c69ff6 to your computer and use it in GitHub Desktop.
Converting coordinates from WGS84 to NAD83-MTM8
This file contains 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
from functools import partial | |
import fiona | |
import pkg_resources | |
import pyproj | |
from fiona.transform import transform as fiona_transform | |
from shapely import ops | |
from shapely.geometry.point import Point | |
# Use python 3.6 | |
# pip install fiona shapely pyproj | |
def _transform_using_fiona(source_crs, target_crs, *points): | |
(xs, ys) = fiona_transform(source_crs, target_crs, [point.x for point in points], [point.y for point in points]) | |
return [Point(x, y) for x, y in zip(xs, ys)] | |
def _transform_using_pyproj(source_crs, target_crs, *points): | |
# Code extracted from geopandas.geoseries.to_crs(self, crs=None, epsg=None) | |
proj_in = pyproj.Proj(source_crs, preserve_units=True) | |
proj_out = pyproj.Proj(target_crs, preserve_units=True) | |
project = partial(pyproj.transform, proj_in, proj_out) | |
return [ops.transform(project, point) for point in points] | |
class TestReProjection(): | |
city_hall_entrance = Point(-73.553785, 45.508722) | |
city_hall_balcony = Point(-73.554138, 45.509080) | |
city_hall_council_chamber = Point(-73.554246, 45.508931) | |
def run(self): | |
mtm8_points_using_fiona = _transform_using_fiona( | |
'EPSG:4326', | |
'EPSG:32188', | |
self.city_hall_entrance, self.city_hall_balcony, self.city_hall_council_chamber | |
) | |
mtm8_points_using_pyproj = _transform_using_pyproj( | |
{'init': 'epsg:4326'}, | |
{'init': 'epsg:32188', 'no_defs': True}, | |
self.city_hall_entrance, self.city_hall_balcony, self.city_hall_council_chamber | |
) | |
print() | |
print(' **** WGS84 points ****') | |
print(f' entrance: {self.city_hall_entrance}') | |
print(f' balcony: {self.city_hall_balcony}') | |
print(f' council chamber: {self.city_hall_council_chamber}') | |
print() | |
print(' ====== Entrance ======') | |
print(f' reprojection using Fiona {fiona.__version__} : {mtm8_points_using_fiona[0]}') | |
print(f' reprojection using pyproj {pyproj.__version__} : {mtm8_points_using_pyproj[0]}') | |
print(f' distance (in meters) : {mtm8_points_using_fiona[0].distance(mtm8_points_using_pyproj[0])}') | |
print(' ====== Balcony ======') | |
print(f' reprojection using Fiona {fiona.__version__} : {mtm8_points_using_fiona[1]}') | |
print(f' reprojection using pyproj {pyproj.__version__} : {mtm8_points_using_pyproj[1]}') | |
print(f' distance (in meters) : {mtm8_points_using_fiona[1].distance(mtm8_points_using_pyproj[1])}') | |
print(' ====== Council Chamber ======') | |
print(f' reprojection using Fiona {fiona.__version__} : {mtm8_points_using_fiona[2]}') | |
print(f' reprojection using pyproj {pyproj.__version__} : {mtm8_points_using_pyproj[2]}') | |
print(f' distance (in meters) : {mtm8_points_using_fiona[2].distance(mtm8_points_using_pyproj[2])}') | |
if __name__ == "__main__": | |
for pkg in pkg_resources.working_set: | |
print(f' - {pkg.key} : {pkg.version}') | |
TestReProjection().run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment