Skip to content

Instantly share code, notes, and snippets.

@galak75
Created March 28, 2019 19:53
Show Gist options
  • Save galak75/772697e119a1c60f28d319ba7e6306d1 to your computer and use it in GitHub Desktop.
Save galak75/772697e119a1c60f28d319ba7e6306d1 to your computer and use it in GitHub Desktop.
Converting poi
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
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()
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