Commit 1e27d72e authored by Daniel Scheffler's avatar Daniel Scheffler
Browse files

Revised geo.projection.WKT2EPSG and added _find_epsgfile() to increase...

Revised geo.projection.WKT2EPSG and added _find_epsgfile() to increase operation system compatibility. Added module test_projection. Updated version info.
parent 1d0b56b4
Pipeline #1066 passed with stages
in 59 seconds
......@@ -7,8 +7,8 @@ from . import numeric
from . import similarity
from . import web
__version__ = '0.4.3'
__versionalias__ = '20170820_01'
__version__ = '0.4.4'
__versionalias__ = '20170909_01'
__author__='Daniel Scheffler'
# Validate GDAL version
......
......@@ -112,15 +112,38 @@ def EPSG2WKT(EPSG_code):
return srs.ExportToWkt()
def WKT2EPSG(wkt, epsg=os.environ['GDAL_DATA'].replace('/gdal', '/proj/epsg')):
def _find_epsgfile():
"""Locate the proj.4 epsg file (defaults to '/usr/local/share/proj/epsg')."""
try:
epsgfile = os.environ['GDAL_DATA'].replace('/gdal', '/proj/epsg')
assert os.path.exists(epsgfile)
except (KeyError, AssertionError):
try:
from pyproj import __file__ as pyprojpath
epsgfile = os.path.join(os.path.dirname(pyprojpath), 'data/epsg')
assert os.path.exists(epsgfile)
except (ImportError, AssertionError):
epsgfile = '/usr/local/share/proj/epsg'
if not os.path.exists(epsgfile):
raise RuntimeError('Could not locate epsg file for converting WKT to EPSG code. '
'Please make sure that your GDAL_DATA environment variable is properly set and the '
'pyproj library is installed.')
return epsgfile
def WKT2EPSG(wkt, epsgfile=''):
""" Transform a WKT string to an EPSG code
:param wkt: WKT definition
:param epsg: the proj.4 epsg file (defaults to '/usr/local/share/proj/epsg')
:param epsgfile: the proj.4 epsg file (automatically located if no path is provided)
:returns: EPSG code
http://gis.stackexchange.com/questions/20298/is-it-possible-to-get-the-epsg-value-from-an-osr-spatialreference-class-using-th
"""
# FIXME this function returns None if datum=NAD27 but works with datum=WGS84, e.g.:
# FIXME {PROJCS["UTM_Zone_33N",GEOGCS["GCS_North_American_1927",DATUM["D_North_American_1927",SPHEROID["Clarke_1866",6378206.4,294.9786982]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]],PROJECTION["Transverse_Mercator"],PARAMETER["False_Easting",500000.0],PARAMETER["False_Northing",0.0],PARAMETER["Central_Meridian",15.0],PARAMETER["Scale_Factor",0.9996],PARAMETER["Latitude_Of_Origin",0.0],UNIT["Meter",1.0]]}
# FIXME {PROJCS["UTM_Zone_33N",GEOGCS["GCS_North_American_1927",DATUM["D_North_American_1927",SPHEROID
# FIXME ["Clarke_1866",6378206.4,294.9786982]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]],
# FIXME PROJECTION["Transverse_Mercator"],PARAMETER["False_Easting",500000.0],PARAMETER["False_Northing",0.0],
# FIXME PARAMETER["Central_Meridian",15.0],PARAMETER["Scale_Factor",0.9996],PARAMETER["Latitude_Of_Origin",0.0],
# FIXME UNIT["Meter",1.0]]}
if not isinstance(wkt,str):
raise TypeError("'wkt' must be a string. Received %s." %type(wkt))
......@@ -141,7 +164,8 @@ def WKT2EPSG(wkt, epsg=os.environ['GDAL_DATA'].replace('/gdal', '/proj/epsg')):
if ac is None: # try brute force approach by grokking proj epsg definition file
p_out = p_in.ExportToProj4()
if p_out:
with open(epsg) as f:
epsgfile = epsgfile or _find_epsgfile()
with open(epsgfile) as f:
for line in f:
if line.find(p_out) != -1:
m = re.search('<(\\d+)>', line)
......@@ -172,4 +196,4 @@ def get_prjLonLat(fmt='wkt'):
assert re.search('wkt',fmt,re.I) or re.search('Proj4',fmt,re.I), 'unsupported output format'
srs = osr.SpatialReference()
srs.ImportFromEPSG(4326)
return srs.ExportToWkt() if re.search('wkt',fmt,re.I) else srs.ExportToProj4()
\ No newline at end of file
return srs.ExportToWkt() if re.search('wkt',fmt,re.I) else srs.ExportToProj4()
......@@ -11,13 +11,13 @@ with open('README.rst') as readme_file:
with open('HISTORY.rst') as history_file:
history = history_file.read()
requirements = ['gdal', 'numpy', 'shapely', 'six', 'rasterio', 'geopandas', 'scikit-image']
requirements = ['gdal', 'numpy', 'shapely', 'six', 'rasterio', 'geopandas', 'scikit-image', 'pyproj']
setup_requirements = [] # TODO(danschef): put setup requirements (distutils extensions, etc.) here
test_requirements = requirements + ['coverage']
setup(
name='py_tools_ds',
version='0.4.3',
version='0.4.4',
description="A collection of Python tools by Daniel Scheffler.",
long_description=readme + '\n\n' + history,
author="Daniel Scheffler",
......
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
test_projection
----------------------------------
Tests for `py_tools_ds.geo.projection` module.
"""
import unittest
from py_tools_ds.geo.projection import WKT2EPSG
class Test_WKT2EPSG(unittest.TestCase):
def setUp(self):
self.wkt_utm = \
"""
PROJCS["WGS 84 / UTM zone 33N",
GEOGCS["WGS 84",
DATUM["WGS_1984",
SPHEROID["WGS 84", 6378137, 298.257223563,
AUTHORITY["EPSG", "7030"]],
AUTHORITY["EPSG", "6326"]],
PRIMEM["Greenwich", 0,
AUTHORITY["EPSG", "8901"]],
UNIT["degree", 0.0174532925199433,
AUTHORITY["EPSG", "9122"]],
AUTHORITY["EPSG", "4326"]],
PROJECTION["Transverse_Mercator"],
PARAMETER["latitude_of_origin", 0],
PARAMETER["central_meridian", 15],
PARAMETER["scale_factor", 0.9996],
PARAMETER["false_easting", 500000],
PARAMETER["false_northing", 0],
UNIT["metre", 1,
AUTHORITY["EPSG", "9001"]],
AXIS["Easting", EAST],
AXIS["Northing", NORTH],
AUTHORITY["EPSG", "32633"]]
"""
def test_UTM_wkt(self):
epsg = WKT2EPSG(self.wkt_utm, epsgfile='')
print('epsg', epsg)
self.assertTrue(isinstance(epsg, int))
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
test_py_tools_ds
----------------------------------
Tests for `py_tools_ds` module.
"""
import sys
import unittest
from py_tools_ds import *
class TestPy_tools_ds(unittest.TestCase):
def setUp(self):
pass
def tearDown(self):
pass
def test_000_something(self):
pass
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment