ecometrica / gdal2mbtiles

Converts a GDAL-readable dataset into an MBTiles file. This is used to generate web maps.
Apache License 2.0
165 stars 27 forks source link

TypeError: in method 'SpatialReference_ImportFromEPSG', argument 2 of type 'int' in gdal.py line 799 #22

Closed alanlun516 closed 6 years ago

alanlun516 commented 6 years ago

Hello again~ Error occurs when converting PNG to MBTiles (whenever calling _osr.SpatialReference_ImportFromEPS actually).

$ gdal2mbtiles test.png test.mbtiles
Traceback (most recent call last):
  File ".../bin/gdal2mbtiles", line 11, in <module>
    sys.exit(main())
  File ".../gdal2mbtiles/main.py", line 309, in main
    colors=colors, band=band)
  File ".../gdal2mbtiles/helpers.py", line 194, in warp_mbtiles
    validate_resolutions(resolution=dataset.GetNativeResolution(),
  File ".../gdal2mbtiles/gdal.py", line 497, in GetNativeResolution
    dst_ref = self.GetSpatialReference()
  File ".../gdal2mbtiles/gdal.py", line 463, in GetSpatialReference
    sr = sr.FromEPSG(sr.GetEPSGCode())
  File ".../gdal2mbtiles/gdal.py", line 799, in FromEPSG
    s.ImportFromEPSG(code)
  File ".../osgeo/osr.py", line 1128, in ImportFromEPSG
    return _osr.SpatialReference_ImportFromEPSG(self, *args)
TypeError: in method 'SpatialReference_ImportFromEPSG', argument 2 of type 'int'

I tried to show what "code" was and therefore added print("code: {}, type(code): {}".format(code, type(code))) to line 799 in gdal.py.

$ gdal2mbtiles test.png test.mbtiles
code: 3857, type(code): <type 'int'>
code: 3857, type(code): <type 'int'>
code: None, type(code): <type 'NoneType'>
Traceback (most recent call last):
  File ".../bin/gdal2mbtiles", line 11, in <module>
    sys.exit(main())
  File ".../gdal2mbtiles/main.py", line 309, in main
    colors=colors, band=band)
  File ".../gdal2mbtiles/helpers.py", line 194, in warp_mbtiles
    validate_resolutions(resolution=dataset.GetNativeResolution(),
  File ".../gdal2mbtiles/gdal.py", line 497, in GetNativeResolution
    dst_ref = self.GetSpatialReference()
  File ".../gdal2mbtiles/gdal.py", line 463, in GetSpatialReference
    sr = sr.FromEPSG(sr.GetEPSGCode())
  File ".../gdal2mbtiles/gdal.py", line 800, in FromEPSG
    s.ImportFromEPSG(code)
  File ".../osgeo/osr.py", line 1128, in ImportFromEPSG
    return _osr.SpatialReference_ImportFromEPSG(self, *args)
TypeError: in method 'SpatialReference_ImportFromEPSG', argument 2 of type 'int'

Therefore I tried to give a --spatial-reference 4326, it shows the following.

$ gdal2mbtiles --spatial-reference 4326 test.png test.mbtiles
code: 4326, type(code): <type 'int'>
code: 3857, type(code): <type 'int'>
code: None, type(code): <type 'NoneType'>
Traceback (most recent call last):
  File ".../bin/gdal2mbtiles", line 11, in <module>
    sys.exit(main())
  File ".../gdal2mbtiles/main.py", line 309, in main
    colors=colors, band=band)
  File ".../gdal2mbtiles/helpers.py", line 194, in warp_mbtiles
    validate_resolutions(resolution=dataset.GetNativeResolution(),
  File ".../gdal2mbtiles/gdal.py", line 497, in GetNativeResolution
    dst_ref = self.GetSpatialReference()
  File ".../gdal2mbtiles/gdal.py", line 463, in GetSpatialReference
    sr = sr.FromEPSG(sr.GetEPSGCode())
  File ".../gdal2mbtiles/gdal.py", line 800, in FromEPSG
    s.ImportFromEPSG(code)
  File ".../osgeo/osr.py", line 1128, in ImportFromEPSG
    return _osr.SpatialReference_ImportFromEPSG(self, *args)
TypeError: in method 'SpatialReference_ImportFromEPSG', argument 2 of type 'int'

Seems that it always fails in the third round. Please help.

mghughes commented 6 years ago

Hey @alanlun516,

I think we may need a bit more information here to figure out what's going on. I've been able to convert a png to mbtiles using the latest version of gdal2mbtiles (2.1.1), so it doesn't seem to be an issue for all PNG files.

Is your PNG georeferenced? Could you paste the output of gdalinfo test.png? And what version of gdal2mbtiles are you running?

alanlun516 commented 6 years ago

Hello,

I am using

  Linux Mint 18.1
  Python 3.6.5
  pip 9.0.3
  gdal2mbtiles 2.1.0

The requested gdalinfo output

$ gdalinfo test.png
Driver: PNG/Portable Network Graphics
Files: test.png
Size is 2048, 1536
Coordinate System is `'
Image Structure Metadata:
  INTERLEAVE=PIXEL
Corner Coordinates:
Upper Left  (    0.0,    0.0)
Lower Left  (    0.0, 1536.0)
Upper Right ( 2048.0,    0.0)
Lower Right ( 2048.0, 1536.0)
Center      ( 1024.0,  768.0)
Band 1 Block=2048x1 Type=Byte, ColorInterp=Red
  Mask Flags: PER_DATASET ALPHA
Band 2 Block=2048x1 Type=Byte, ColorInterp=Green
  Mask Flags: PER_DATASET ALPHA
Band 3 Block=2048x1 Type=Byte, ColorInterp=Blue
  Mask Flags: PER_DATASET ALPHA
Band 4 Block=2048x1 Type=Byte, ColorInterp=Alpha

Thanks, Alan

mghughes commented 6 years ago

Thanks for the info @alanlun516!

Unless there's a copy/paste error there, it seems like your PNG is missing some projection and datum information about the coordinate system? So I think the issue may be with your input file, since GDAL can't read the coordinate system info (and thus gdal2mbtiles' SpatialReference can't read the EPSG code)

alanlun516 commented 6 years ago

Hi @mghughes,

It is not an error, sorry that I misused the program. I think the issue is not valid. I would like to suggest a warning about coordinate system information missing. Thanks a lots @mghughes.

Best, Alan