creare-com / pydem

Python library for Global Hydrology Analysis. Used to calculate upstream contributing area, aspect, slope, and topographic wetness index.
Apache License 2.0
114 stars 35 forks source link

KeyError reading coordinate strings #1

Closed emilopez closed 8 years ago

emilopez commented 9 years ago

I cant read a dem file.

I have installed pydem using pip and all the requirements (numba, etc) via conda.

When I try

dem_proc = DEMProcessor(filename_to_elevation_geotiff)

I am receiving a key error with an etopo dem:

/home/emiliano/anaconda/lib/python2.7/site-packages/pydem/reader/gdal_reader.pyc in _get_gridcoordinates(self) 128 129 wkt = dataset.GetProjection() --> 130 wkt = d_wkt_toname[wkt] 131 132

KeyError: 'GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.2572235604902,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0],UNIT["degree",0.0174532925199433],AUTHORITY["EPSG","4326"]]'

And, similar error message for aster dem:

KeyError: 'GEOGCS["WGS 84",DATUM["unknown",SPHEROID["WGS84",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["degree",0.0174532925199433]]'

I have also tried converting .bil to .tiff using gdal_convert, and I get the same keyerror message.

mpu-creare commented 9 years ago

It looks like the coordinate strings being read by gdal is slightly different between our setups... I'll have to find a more robust method to extract that information. For now, to get you going, try adding the following line after line 34 of the pydem.reader.my_types.py: Currently is:

d_wkt_to_name[r'GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0],UNIT["degree",0.0174532925199433],AUTHORITY["EPSG","4326"]]'] = 'WGS84'  

Please add

d_wkt_to_name[r'GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS84",6378137,298.2572235604902,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0],UNIT["degree",0.0174532925199433],AUTHORITY["EPSG","4326"]]''] = 'WGS84'

Let me know if that works and I'll try to make this more robust.

emilopez commented 9 years ago

An invalid syntax error, I think a small typo could be, so I remove the last single quote inside clasp:

d_wkt_to_name[r'GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS84",6378137,298.2572235604902,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0],UNIT["degree",0.0174532925199433],AUTHORITY["EPSG","4326"]]'] = 'WGS84'

Now, I get the following error message:

File "/home/emiliano/data/DI/Materias/2015/geomorfometria/DEMystiPy/DEMystiPy/test_pydem.py", line 4, in dem_proc = DEMProcessor(filename_to_elevation_geotiff) File "/home/emiliano/anaconda/lib/python2.7/site-packages/pydem/dem_processing.py", line 571, in init shp = np.array(self.data.shape) - 1 AttributeError: 'DEMProcessor' object has no attribute 'data',

mpu-creare commented 9 years ago

That's not a helpful error message! I'll fix that, it should throw a "File not found" error... I think what happened is this was false:

if os.path.exists(file_name):

What OS are you on? Try using the absolute path to the filename.

emilopez commented 9 years ago

OMG, i failed the path, sorry. I am using debian linux stable.

Mi code is:

import os
from pydem.dem_processing import DEMProcessor

file_name = 'img_dem/aster30.tiff'
if os.path.exists(file_name):
    dem_proc = DEMProcessor(file_name)

I deleted the last single quote because the syntax error, thats right? Now, the following error:

Traceback (most recent call last):
  File "/home/emiliano/data/DI/Materias/2015/geomorfometria/DEMystiPy/test_pydem.py", line 6, in <module>
    dem_proc = DEMProcessor(file_name)
  File "/home/emiliano/anaconda/lib/python2.7/site-packages/pydem/dem_processing.py", line 556, in __init__
    elev, = elev_file.raster_layers
  File "/home/emiliano/anaconda/lib/python2.7/site-packages/traits/has_traits.py", line 1126, in decorator
    self.__dict__[ name ] = result = function( self )
  File "/home/emiliano/anaconda/lib/python2.7/site-packages/pydem/reader/gdal_reader.py", line 195, in _get_raster_layers
    for raster_band in raster_bands]
  File "/home/emiliano/anaconda/lib/python2.7/site-packages/pydem/reader/gdal_reader.py", line 143, in _raster_layer_from_raster_band
    layer.grid_coordinates = self.grid_coordinates
  File "/home/emiliano/anaconda/lib/python2.7/site-packages/traits/has_traits.py", line 1126, in decorator
    self.__dict__[ name ] = result = function( self )
  File "/home/emiliano/anaconda/lib/python2.7/site-packages/pydem/reader/gdal_reader.py", line 130, in _get_grid_coordinates
    wkt = d_wkt_to_name[wkt_]
KeyError: 'GEOGCS["WGS 84",DATUM["unknown",SPHEROID["WGS84",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["degree",0.0174532925199433]]'
mpu-creare commented 9 years ago

Interesting, this is yet another version of the wkt name... this time it looks like the datum is not properly defined. You can try the same trick as before by adding this key to the dictionary.

d_wkt_to_name['GEOGCS["WGS 84",DATUM["unknown",SPHEROID["WGS84",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["degree",0.0174532925199433]]'] = 'WGS84'

Alternatively, you could try (from the commandline) to convert the file to WGS84 (which should fix the datum definition).

gdalwarp -co BIGTIFF=YES -of GTiff -co compress=lzw -ot Int16 -co TILED=YES -wo OPTIMIZE_SIZE=YES -r near -t_srs EPSG:4326 aster30.tiff aster30WGS84.tiff

Thanks for the patience and effort trying to use our very alpha package. I suspect that you'll have some more problems down the line on linux... but I can try to fix those as well when the come up.

emilopez commented 9 years ago

Great Matt! Now its working! I have tried using .tiff and .bil. with etopo and aster dems and everything was fine.

I am developing a small GUI for gdaldem and I would like to include some calcs of pyDEM. Really Thanks!

mpu-creare commented 9 years ago

Excellent, glad you got it working.

I do suggest looking at pygeoprocessing, I'm thinking of moving some of pydem's functionality over there to consolidate efforts.

emilopez commented 9 years ago

Yes, I recently installed it and I 'm going to test it. Thanks again!

emilopez commented 9 years ago

If you prefer I can do a fork and a pull request, so you can merge the modifications... Let me know.

mpu-creare commented 9 years ago

That would be great. Please do so.


From: Emiliano [notifications@github.com] Sent: Friday, August 07, 2015 5:48 PM To: creare-com/pydem Cc: Matt Ueckermann Subject: Re: [pydem] KeyError reading coordinate strings (#1)

If you prefer I can do a fork and a pull request, so you can merge the modifications... Let me know.

� Reply to this email directly or view it on GitHubhttps://github.com/creare-com/pydem/issues/1#issuecomment-128840469.

MortimerSnerdly commented 6 years ago

Hey Guys, has this fix been added to the main code?

mpu-creare commented 6 years ago

It did. Made it on to the master branch with the merge request referenced above.