Skip to content

Commit

Permalink
Extract lon_lat_to_easting_northing and easting_northing_to_lon_lat
Browse files Browse the repository at this point in the history
as utility functions
  • Loading branch information
jonmmease committed Nov 16, 2020
1 parent ab29572 commit 5c0a4a9
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 55 deletions.
62 changes: 7 additions & 55 deletions holoviews/element/tiles.py
Expand Up @@ -8,6 +8,7 @@
from ..core import util
from ..core.dimension import Dimension
from ..core.element import Element2D
from ..util.transform import lon_lat_to_easting_northing, easting_northing_to_lon_lat


class Tiles(Element2D):
Expand Down Expand Up @@ -60,70 +61,21 @@ def lon_lat_to_easting_northing(longitude, latitude):
Projects the given longitude, latitude values into Web Mercator
(aka Pseudo-Mercator or EPSG:3857) coordinates.
Longitude and latitude can be provided as scalars, Pandas columns,
or Numpy arrays, and will be returned in the same form. Lists
or tuples will be converted to Numpy arrays.
Args:
longitude
latitude
Returns:
(easting, northing)
Examples:
easting, northing = lon_lat_to_easting_northing(-74,40.71)
easting, northing = lon_lat_to_easting_northing(
np.array([-74]),np.array([40.71])
)
df=pandas.DataFrame(dict(longitude=np.array([-74]),latitude=np.array([40.71])))
df.loc[:, 'longitude'], df.loc[:, 'latitude'] = lon_lat_to_easting_northing(
df.longitude,df.latitude
)
See docstring for holoviews.util.transform.lon_lat_to_easting_northing
for more information
"""
if isinstance(longitude, (list, tuple)):
longitude = np.array(longitude)
if isinstance(latitude, (list, tuple)):
latitude = np.array(latitude)

origin_shift = np.pi * 6378137
easting = longitude * origin_shift / 180.0
with np.errstate(divide='ignore', invalid='ignore'):
northing = np.log(
np.tan((90 + latitude) * np.pi / 360.0)
) * origin_shift / np.pi
return easting, northing
return lon_lat_to_easting_northing(longitude, latitude)

@staticmethod
def easting_northing_to_lon_lat(easting, northing):
"""
Projects the given easting, northing values into
longitude, latitude coordinates.
easting and northing values are assumed to be in Web Mercator
(aka Pseudo-Mercator or EPSG:3857) coordinates.
Args:
easting
northing
Returns:
(longitude, latitude)
See docstring for holoviews.util.transform.easting_northing_to_lon_lat
for more information
"""
if isinstance(easting, (list, tuple)):
easting = np.array(easting)
if isinstance(northing, (list, tuple)):
northing = np.array(northing)

origin_shift = np.pi * 6378137
longitude = easting * 180.0 / origin_shift
with np.errstate(divide='ignore'):
latitude = np.arctan(
np.exp(northing * np.pi / origin_shift)
) * 360.0 / np.pi - 90
return longitude, latitude
return easting_northing_to_lon_lat(easting, northing)


# Mapping between patterns to match specified as tuples and tuples containing attributions
Expand Down
71 changes: 71 additions & 0 deletions holoviews/util/transform.py
Expand Up @@ -859,3 +859,74 @@ def _coerce(self, dataset):
if self.interface_applies(dataset, coerce=False):
return dataset
return dataset.clone(datatype=['xarray'])


def lon_lat_to_easting_northing(longitude, latitude):
"""
Projects the given longitude, latitude values into Web Mercator
(aka Pseudo-Mercator or EPSG:3857) coordinates.
Longitude and latitude can be provided as scalars, Pandas columns,
or Numpy arrays, and will be returned in the same form. Lists
or tuples will be converted to Numpy arrays.
Args:
longitude
latitude
Returns:
(easting, northing)
Examples:
easting, northing = lon_lat_to_easting_northing(-74,40.71)
easting, northing = lon_lat_to_easting_northing(
np.array([-74]),np.array([40.71])
)
df=pandas.DataFrame(dict(longitude=np.array([-74]),latitude=np.array([40.71])))
df.loc[:, 'longitude'], df.loc[:, 'latitude'] = lon_lat_to_easting_northing(
df.longitude,df.latitude
)
"""
if isinstance(longitude, (list, tuple)):
longitude = np.array(longitude)
if isinstance(latitude, (list, tuple)):
latitude = np.array(latitude)

origin_shift = np.pi * 6378137
easting = longitude * origin_shift / 180.0
with np.errstate(divide='ignore', invalid='ignore'):
northing = np.log(
np.tan((90 + latitude) * np.pi / 360.0)
) * origin_shift / np.pi
return easting, northing


def easting_northing_to_lon_lat(easting, northing):
"""
Projects the given easting, northing values into
longitude, latitude coordinates.
easting and northing values are assumed to be in Web Mercator
(aka Pseudo-Mercator or EPSG:3857) coordinates.
Args:
easting
northing
Returns:
(longitude, latitude)
"""
if isinstance(easting, (list, tuple)):
easting = np.array(easting)
if isinstance(northing, (list, tuple)):
northing = np.array(northing)

origin_shift = np.pi * 6378137
longitude = easting * 180.0 / origin_shift
with np.errstate(divide='ignore'):
latitude = np.arctan(
np.exp(northing * np.pi / origin_shift)
) * 360.0 / np.pi - 90
return longitude, latitude

0 comments on commit 5c0a4a9

Please sign in to comment.