1

I have written some code to export projection information from geoPDF’s. For some reason on some of the files I get the following error and other are fine.

    AttributeError: 'NoneType' object has no attribute 'GetLayer'

As I understand the above error it means that the proj variable is empty. When I check the files with GDALINFO it clearly shows the projection information

My Code

    fileName = 'filename'
    filePath = 'path/to/file/'

    driver = ogr.GetDriverByName('PDF')
    dataset = driver.Open(filePath + fileName +'.pdf')

    # from Layer
    layer = dataset.GetLayer()
    spatialRef = layer.GetSpatialRef()
    # from Geometry
    feature = layer.GetNextFeature()
    geom = feature.GetGeometryRef()
    spatialRef = geom.GetSpatialReference()

    spatialRef.MorphToESRI()
    file = open(filePath + fileName +'.prj', 'w')
    file.write(spatialRef.ExportToWkt())
    file.close()

    print (neatline_wkt)
    print (spatialRef)

Some PDF files raise the following error but others don't. For example this file has projection information embedded as per below - https://usfs-public.box.com/shared/static/26rhbxx7oqxdtw5efxi5asd894cwbmwb.pdf but causes an error.

    gdalinfo CA_Plumas_National_Forest_Greenville_MVUM.pdf
    Driver: PDF/Geospatial PDF
    Files: CA_Plumas_National_Forest_Greenville_MVUM.pdf
    Size is 5250, 3412
    Coordinate System is:
    PROJCS["NAD_1983_UTM_Zone_10N",
        GEOGCS["GCS_North_American_1983",
            DATUM["North_American_Datum_1983",
                SPHEROID["GRS_1980",6378137.0,298.257222101]],
    PRIMEM["Greenwich",0.0],
    UNIT["Degree",0.0174532925199433]],
    PROJECTION["Transverse_Mercator"],
    PARAMETER["False_Easting",500000.0],
    PARAMETER["False_Northing",0.0],
    PARAMETER["Central_Meridian",-123.0],
    PARAMETER["Scale_Factor",0.9996],
    PARAMETER["Latitude_Of_Origin",0.0],
    UNIT["Meter",1.0]]
    GeoTransform =
     620218.9230522923, 14.39324062623421, -4.455847902786653e-06
     4470454.925909669, 0.0001940513634986329, -14.39592969090605
   Metadata:
     CREATION_DATE=D:20190529162113-08
     CREATOR=Esri ArcMap 10.5.1.7333
     NEATLINE=POLYGON ((649617.508764131 4422834.69664129,649617.465150077 
     4468987.38965106,694136.387837796 4468987.60928566,694136.315653489 
     4422835.67742383,649617.508764131 4422834.69664129))
     Corner Coordinates:
     Upper Left  (  620218.923, 4470454.926) (121d35' 1.84"W, 40d22'33.65"N)
     Lower Left  (  620218.908, 4421336.014) (121d35'34.82"W, 39d56' 0.99"N)
     Upper Right (  695783.436, 4470455.945) (120d41'39.34"W, 40d21'42.13"N)
     Lower Right (  695783.421, 4421337.033) (120d42'33.00"W, 39d55'10.27"N)
     Center      (  658001.172, 4445895.979) (121d 8'42.09"W, 40d 8'54.83"N)
     Band 1 Block=5250x1 Type=Byte, ColorInterp=Red
     Band 2 Block=5250x1 Type=Byte, ColorInterp=Green
     Band 3 Block=5250x1 Type=Byte, ColorInterp=Blue

Similar file - https://usfs-public.box.com/shared/static/jv6cf5tp30smo4t1kei9i807xjbnxrmm.pdf has no issues and can create projection file.

Any clues on how to resolve?

  • Are you sure the filepath is valid? For example change dataset = driver.Open(filePath + fileName +'.pdf') to dataset = driver.Open(os.path.join(filePath + fileName +'.pdf')) – BERA Oct 26 at 9:48
  • 1
    @BERA Same issue when change file path. – Mac Maclean Oct 26 at 10:01
  • OGR can't open the pdf and returns None. Set ogr.UseExceptions() for more info. gdal.org/api/… – user2856 Oct 26 at 10:34
  • The code is working fine on the majority of PDF's. For some PDF's it returns NONE for some. OGR still exports shape files from thosePDF's that it can't create prj file from – Mac Maclean Oct 26 at 10:51
  • Set UseExceptions – user2856 Oct 26 at 10:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.