Exemple #1
0
def osr_esri_23():

    result = 'success'

    # Test GEOGCSCS defs
    result1 = osr_esri_test_ogc_esri_ogc(gdal.FindFile('gdal', 'gcs.csv'),
                                         'epsg_gcs2', 'GEOGCS', True)
    if result1 == 'fail':
        result = 'expected_fail'

    # Test PROJCS defs
    result2 = osr_esri_test_ogc_esri_ogc(gdal.FindFile('gdal', 'pcs.csv'),
                                         'epsg_pcs2', 'DATUM', False)
    if result2 == 'fail':
        result = 'fail'

    return result
Exemple #2
0
def osr_esri_test_esri_ogc_esri(ifile,
                                ofile_base,
                                fix_config='NO',
                                check_epsg=False):

    if not os.path.exists(ifile):
        print('input file ' + ifile + ' does not exist')
        return 'fail'

    result = 'success'
    check_srs = True
    check_wkt = False
    failed_epsg_count = 0
    failed_srs_count = 0
    failed_wkt_count = 0
    ofile_epsg = 'tmp/' + ofile_base + '_epsg.txt'
    ofile_srs = 'tmp/' + ofile_base + '_srs.txt'
    ofile_wkt = 'tmp/' + ofile_base + '_wkt.txt'

    #initialise output files
    if not os.path.exists('tmp'):
        os.mkdir('tmp')
    if os.path.exists(ofile_epsg):
        os.unlink(ofile_epsg)
    if check_epsg:
        epsg_ne = ''
        epsg_none = ''
        epsg_other = ''
        of_epsg = open(ofile_epsg, 'w')
    if os.path.exists(ofile_srs):
        os.unlink(ofile_srs)
    if check_srs:
        of_srs = open(ofile_srs, 'w')
    if os.path.exists(ofile_wkt):
        os.unlink(ofile_wkt)
    if check_wkt:
        of_wkt = open(ofile_wkt, 'w')

    #open input file
    if os.path.splitext(ifile)[1] == '.gz':
        f = gzip.open(ifile, 'rb')
    else:
        f = open(ifile, 'rt')
    csv_reader = csv.DictReader(f, delimiter=';')

    epsg_reader = csv.DictReader(gdal.FindFile('gdal', 'gcs.csv'), 'epsg_gcs2',
                                 'GEOGCS', True)

    #set GDAL_FIX_ESRI_WKT option
    fix_config_bak = gdal.GetConfigOption('GDAL_FIX_ESRI_WKT')
    gdal.SetConfigOption('GDAL_FIX_ESRI_WKT', fix_config)

    #need to be quiet because some codes raise errors
    gdal.PushErrorHandler('CPLQuietErrorHandler')

    #parse all lines
    for iline in csv_reader:

        epsg_code = int(iline['COORD_REF_SYS_CODE'])
        if iline['WKT'] is None or iline['WKT'] == '':
            continue

        #read wkt and morph from ESRI
        srs1 = osr.SpatialReference()
        srs1.ImportFromWkt(iline['WKT'])
        wkt1 = srs1.ExportToWkt()
        srs2 = srs1.Clone()
        srs2.MorphFromESRI()
        wkt2 = srs2.ExportToWkt()

        #morph back to ESRI
        srs3 = srs2.Clone()
        srs3.MorphToESRI()
        wkt3 = srs3.ExportToWkt()

        #manage special cases of PROJECTION parameters that have multiple mappings
        remap_proj=dict([ ['Transverse_Mercator','Gauss_Kruger'], ['Equidistant_Cylindrical', 'Plate_Carree'], \
                              ['Hotine_Oblique_Mercator_Azimuth_Natural_Origin','Hotine_Oblique_Mercator_Azimuth_Center'] ] )
        proj1 = srs1.GetAttrValue('PROJCS|PROJECTION')
        proj3 = srs3.GetAttrValue('PROJCS|PROJECTION')
        if proj3 in remap_proj and proj1 == remap_proj[proj3]:
            srs3.SetAttrValue('PROJCS|PROJECTION', remap_proj[proj3])
            wkt3 = srs3.ExportToWkt()

        #check epsg
        if check_epsg:
            epsg2 = srs2.GetAuthorityCode('GEOGCS')
            if epsg2 is None or int(epsg2) != epsg_code:
                #check why epsg codes conflict
                srs4 = osr.SpatialReference()
                #check if EPSG code imports ok
                if srs4.ImportFromEPSG(epsg_code) != 0:
                    #of_epsg.write( 'ERROR: #'+str(epsg_code)+', EPSG does not exist\n')
                    epsg_ne = epsg_ne + ' ' + str(epsg_code)
                else:
                    if epsg2 is None:
                        of_epsg.write('ERROR: #' + str(epsg_code) +
                                      ', did not get EPSG code\n')
                        epsg_none = epsg_none + ' ' + str(epsg_code)
                    else:
                        of_epsg.write('ERROR: EPSG not matching for # ' +
                                      str(epsg_code) + ', got EPSG:' +
                                      str(epsg2) + '\n')
                        epsg_other = epsg_other + ' ' + str(epsg_code)
                    failed_epsg_count = failed_epsg_count + 1
                    of_epsg.write('wkt1: ' + wkt1 + '\n' + 'wkt3: ' + wkt3 +
                                  '\n')

        #check srs
        if check_srs and not srs1.IsSame(srs3):
            failed_srs_count = failed_srs_count + 1
            of_srs.write('ERROR: SRS not matching for # ' +
                         iline['COORD_REF_SYS_CODE'] + '\n')
            of_srs.write('wkt1: ' + wkt1 + '\n' + 'wkt3: ' + wkt3 + '\n')

        #check wkt
        if check_wkt and wkt1 != wkt3:
            failed_wkt_count = failed_wkt_count + 1
            of_wkt.write('WARNING: WKT not matching for # ' +
                         iline['COORD_REF_SYS_CODE'] + '\n')
            of_wkt.write('wkt1: ' + wkt1 + '\n' + 'wkt3: ' + wkt3 + '\n')

    #revert
    gdal.SetConfigOption('GDAL_FIX_ESRI_WKT', fix_config)
    gdal.PopErrorHandler()

    #close files and report
    if check_epsg:
        of_epsg.close()
        if failed_epsg_count > 0:
            print('ERROR: Failed %d EPSG tests, see file %s' %
                  (failed_epsg_count, ofile_epsg))
            #print('epsg_ne: '+epsg_ne)
            #print('epsg_none: '+epsg_none)
            #print('epsg_other: '+epsg_other)
            result = 'fail'
        else:
            os.unlink(ofile_epsg)

    if check_srs:
        of_srs.close()
        if failed_srs_count > 0:
            print('ERROR: Failed %d SRS tests, see file %s' %
                  (failed_srs_count, ofile_srs))
            result = 'fail'
        else:
            os.unlink(ofile_srs)

    if check_wkt:
        of_wkt.close()
        if failed_wkt_count > 0:
            print('WARNING: Failed %d WKT tests, see file %s' %
                  (failed_wkt_count, ofile_wkt))
        else:
            os.unlink(ofile_wkt)

    return result