Skip to content

Instantly share code, notes, and snippets.

@pmav99
Created December 18, 2020 23:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pmav99/f23e9a510a7e3de2c00254bf8c8a8008 to your computer and use it in GitHub Desktop.
Save pmav99/f23e9a510a7e3de2c00254bf8c8a8008 to your computer and use it in GitHub Desktop.
Convert epsg:4326 to epsg:3857 using the latest version of pyproj
import numpy as np
import pyproj
# Use the "new" numpy API for the random number generation (requires numpy >=1.17)
rng = np.random.default_rng(seed=1)
# Create arrays with random points
no_points = 5
longitudes = rng.uniform(low=10, high=20, size=no_points)
latitudes = rng.uniform(low=33, high=45, size=no_points)
print(" Longitude \t Latitude")
print(np.column_stack((longitudes, latitudes)))
print()
# Create the transformer object.
# You can create the object using either integers or "epsg" strings
transformer = pyproj.Transformer.from_crs(crs_from=4326, crs_to=3857)
# pyproj.Transformer.from_crs(crs_from="epsg:4326", crs_to="epsg:3857")
# Apply the transformation.
# Be careful! The order of the output here is Y/X
y1, x1 = transformer.transform(longitudes, latitudes)
print(np.column_stack((x1, y1)))
print()
# If you prefer to work with X/Y instead you need to set `always_xy=True` to the transformer object
transformer = pyproj.Transformer.from_crs(crs_from=4326, crs_to=3857, always_xy=True)
x2, y2 = transformer.transform(longitudes, latitudes)
print(np.column_stack((x2, y2)))
print()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment