Exemplo n.º 1
0
def test_gdal_EscapeString_errors():

    if sys.maxsize > 2**32:
        pytest.skip('Test not available on 64 bit')

    # Allocation will be < 4 GB, but will fail being > 2 GB
    assert gdal.EscapeString(b'"' *
                             (((1 << 32) - 1) // 6), gdal.CPLES_XML) is None

    # Allocation will be > 4 GB
    assert gdal.EscapeString(b'"' * (((1 << 32) - 1) // 6 + 1),
                             gdal.CPLES_XML) is None
Exemplo n.º 2
0
def createVRT(vrtfile, bands):
    
    ds = gdal.Open( bands[0][0] )
    xsize = ds.RasterXSize
    ysize = ds.RasterYSize
    gt = ds.GetGeoTransform()
    srs = ds.GetProjectionRef()
    ds = None

    vrt = '''<VRTDataset rasterXSize="{0}" rasterYSize="{1}">
    <SRS>{2}</SRS>
    <GeoTransform>{3:.15f}, {4:.15f}, {5:.15f}, {6:.15f}, {7:.15f}, {8:.15f}</GeoTransform>'''\
      .format(xsize, ysize, gdal.EscapeString(srs, scheme = gdal.CPLES_XML), \
      gt[0], gt[1], gt[2], gt[3], gt[4], gt[5])

    dstBand = 1
    for b in bands:
        # open file and get info from it
        ds = gdal.Open( b[0] )
        dataType = ds.GetRasterBand(b[1]).DataType
        dataTypeName = gdal.GetDataTypeName(dataType)
        ds = None

        # add this band to the vrt
        vrt = vrt + '''
          <VRTRasterBand dataType="{0}" band="{1:d}">
            <ColorInterp>{2}</ColorInterp>
            <SimpleSource>
            <SourceFilename relativeToVRT="1">{3}</SourceFilename>
            <SourceBand>{4:d}</SourceBand>
            <SrcRect xOff="0" yOff="0" xSize="{5:d}" ySize="{6:d}"/>
            <DstRect xOff="0" yOff="0" xSize="{5:d}" ySize="{6:d}"/>
            </SimpleSource>
          </VRTRasterBand>'''.format( \
            dataTypeName, dstBand, b[2], b[0], b[1], \
            xsize, ysize)
        dstBand = dstBand + 1

    vrt = vrt + '''
</VRTDataset>'''

    # create an in memory VRT file from the XML
    vrt_ds = gdal.Open( vrt )
    # and copy it to a file
    driver = gdal.GetDriverByName('VRT')
    dst_ds = driver.CreateCopy( vrtfile, vrt_ds )

    dst_ds = None
Exemplo n.º 3
0
def Esc(x):
    return gdal.EscapeString( x, gdal.CPLES_XML )
Exemplo n.º 4
0
def _Esc(x):
    return gdal.EscapeString(x, gdal.CPLES_XML).decode('UTF-8')
Exemplo n.º 5
0
def trHandleCode(code, gen_dict_line, report_error, output_format):

    try:
        err = prj_srs.ImportFromEPSG(code)
    except:
        err = 1

    if err != 0 and report_error:
        print('Unable to lookup %d, either not a valid EPSG' % code)
        print('code, or it the EPSG CSV files are not accessible.')
        sys.exit(2)
    else:
        if output_format == '-pretty_wkt':
            if gen_dict_line:
                print('EPSG:%d' % code)

            print(prj_srs.ExportToPrettyWkt())

        if output_format == '-xml':
            print(prj_srs.ExportToXML())

        if output_format == '-wkt':
            if gen_dict_line:
                print('EPSG:%d' % code)

            print(prj_srs.ExportToWkt())

        if output_format == '-proj4':
            out_string = prj_srs.ExportToProj4()

            name = prj_srs.GetAttrValue('COMPD_CS')
            if name is None:
                name = prj_srs.GetAttrValue('PROJCS')
            if name is None:
                name = prj_srs.GetAttrValue('GEOGCS')
            if name is None:
                name = prj_srs.GetAttrValue('GEOCCS')

            if name is None:
                name = 'Unknown'

            print('# %s' % name)
            if err == 0 and out_string.find('+proj=') > -1:
                print('<%s> %s <>' % (str(code), out_string))
            else:
                print('# Unable to translate coordinate system '
                      'EPSG:%d into PROJ.4 format.' % code)
                print('#')

        if output_format == '-postgis':

            name = prj_srs.GetAttrValue('COMPD_CS')
            if name is None:
                name = prj_srs.GetAttrValue('PROJCS')
            if name is None:
                name = prj_srs.GetAttrValue('GEOGCS')
            if name is None:
                name = prj_srs.GetAttrValue('GEOCCS')

            try:
                proj4text = prj_srs.ExportToProj4()
            except:
                err = 1
            wkt = prj_srs.ExportToWkt()

            print('---')
            print('--- EPSG %d : %s' % (code, name))
            print('---')

            if err:
                print('-- (unable to translate)')
            else:
                wkt = gdal.EscapeString(wkt, scheme=gdal.CPLES_SQL)
                proj4text = gdal.EscapeString(proj4text, scheme=gdal.CPLES_SQL)
                print('INSERT INTO "spatial_ref_sys" ("srid","auth_name","auth_srid","srtext","proj4text") VALUES (%s,\'EPSG\',%s,\'%s\',\'%s\');' % \
                      (str(code),str(code),wkt,proj4text))

        # INGRES COPY command input.
        if output_format == '-copy':

            try:
                wkt = prj_srs.ExportToWkt()
                proj4text = prj_srs.ExportToProj4()

                print( '%d\t%d%s\t%d\t%d%s\t%d%s\n' \
                       % (code,4,'EPSG',code,len(wkt),wkt,
                          len(proj4text),proj4text))
            except:
                pass
Exemplo n.º 6
0
def test_gdal_EscapeString():

    assert gdal.EscapeString(b'', gdal.CPLES_XML) == ''

    assert gdal.EscapeString(b'&', gdal.CPLES_XML) == '&amp;'

    assert gdal.EscapeString(b'<', gdal.CPLES_XML) == '&lt;'

    assert gdal.EscapeString(b'>', gdal.CPLES_XML) == '&gt;'

    assert gdal.EscapeString(b'"', gdal.CPLES_XML) == '&quot;'

    assert gdal.EscapeString(b'\xEF\xBB\xBF', gdal.CPLES_XML) == '&#xFEFF;'

    assert gdal.EscapeString(b'\t', gdal.CPLES_XML) == '\t'

    assert gdal.EscapeString(b'\n', gdal.CPLES_XML) == '\n'

    assert gdal.EscapeString(b'\x01a', gdal.CPLES_XML) == 'a'

    assert gdal.EscapeString(b'', gdal.CPLES_XML_BUT_QUOTES) == ''

    assert gdal.EscapeString(b'&', gdal.CPLES_XML_BUT_QUOTES) == '&amp;'

    assert gdal.EscapeString(b'<', gdal.CPLES_XML_BUT_QUOTES) == '&lt;'

    assert gdal.EscapeString(b'>', gdal.CPLES_XML_BUT_QUOTES) == '&gt;'

    assert gdal.EscapeString(b'"', gdal.CPLES_XML_BUT_QUOTES) == '"'

    assert gdal.EscapeString(b'\xEF\xBB\xBF',
                             gdal.CPLES_XML_BUT_QUOTES) == '&#xFEFF;'

    assert gdal.EscapeString(b'\t', gdal.CPLES_XML_BUT_QUOTES) == '\t'

    assert gdal.EscapeString(b'\n', gdal.CPLES_XML_BUT_QUOTES) == '\n'

    assert gdal.EscapeString(b'\x01a', gdal.CPLES_XML_BUT_QUOTES) == 'a'

    assert gdal.EscapeString(b'', gdal.CPLES_BackslashQuotable) == ''

    assert gdal.EscapeString(b'a', gdal.CPLES_BackslashQuotable) == 'a'

    assert gdal.EscapeString(b'\x00x', gdal.CPLES_BackslashQuotable) == '\\0x'

    assert gdal.EscapeString(b'\x01', gdal.CPLES_BackslashQuotable) == '\x01'

    assert gdal.EscapeString(b'\\', gdal.CPLES_BackslashQuotable) == '\\\\'

    assert gdal.EscapeString(b'\n', gdal.CPLES_BackslashQuotable) == '\\n'

    assert gdal.EscapeString(b'"', gdal.CPLES_BackslashQuotable) == '\\"'

    assert gdal.EscapeString(b'', gdal.CPLES_URL) == ''

    assert gdal.EscapeString(b'aZAZ09$-_.+!*\'(), ',
                             gdal.CPLES_URL) == 'aZAZ09$-_.+!*\'(),%20'

    assert gdal.EscapeString(b"", gdal.CPLES_SQL) == ""

    assert gdal.EscapeString(b"a", gdal.CPLES_SQL) == "a"

    assert gdal.EscapeString(b"a'a", gdal.CPLES_SQL) == "a''a"

    assert gdal.EscapeString(b"", gdal.CPLES_CSV) == ""

    assert gdal.EscapeString(b"a'b", gdal.CPLES_CSV) == "a'b"

    assert gdal.EscapeString(b'a"b', gdal.CPLES_CSV) == '"a""b"'

    assert gdal.EscapeString(b'a,b', gdal.CPLES_CSV) == '"a,b"'

    assert gdal.EscapeString(b'a,b', gdal.CPLES_CSV) == '"a,b"'

    assert gdal.EscapeString(b'a\tb', gdal.CPLES_CSV) == '"a\tb"'

    assert gdal.EscapeString(b'a\nb', gdal.CPLES_CSV) == '"a\nb"'

    assert gdal.EscapeString(b'a\rb', gdal.CPLES_CSV) == '"a\rb"'
Exemplo n.º 7
0
def trHandleCode(set_srid, srs, auth_name, code, deprecated, output_format):

    if output_format == '-pretty_wkt':
        print('%s:%s' % (auth_name, str(code)))
        print(srs.ExportToPrettyWkt())

    if output_format == '-xml':
        print(srs.ExportToXML())

    if output_format == '-wkt':
        print('EPSG:%d' % code)
        print(srs.ExportToWkt())

    if output_format == '-proj4':
        out_string = srs.ExportToProj4()

        name = srs.GetName()

        print('# %s' % name)
        if out_string.find('+proj=') > -1:
            print('<%s> %s <>' % (str(code), out_string))
        else:
            print('# Unable to translate coordinate system '
                    '%s:%s into PROJ.4 format.' % (auth_name, str(code)))
            print('#')

    if output_format == '-postgis':

        if code in set_srid:
            if auth_name == 'ESRI':
                if int(code) < 32767:
                    return
        assert code not in set_srid, (auth_name, code)
        set_srid.add(code)

        name = srs.GetName()
        if deprecated and 'deprecated' not in name:
            name += " (deprecated)"
        wkt = srs.ExportToWkt()
        proj4text = srs.ExportToProj4()

        print('---')
        print('--- %s %s : %s' % (auth_name, str(code), name))
        print('---')

        if proj4text is None or len(proj4text) == 0:
            print('-- (unable to translate to PROJ.4)')
        else:
            wkt = gdal.EscapeString(wkt, scheme=gdal.CPLES_SQL)
            proj4text = gdal.EscapeString(proj4text, scheme=gdal.CPLES_SQL)
            print('INSERT INTO "spatial_ref_sys" ("srid","auth_name","auth_srid","srtext","proj4text") VALUES (%d,\'%s\',%d,\'%s\',\'%s\');' %
                    (int(code), auth_name, int(code), wkt, proj4text))

    # INGRES COPY command input.
    if output_format == '-copy':

        try:
            wkt = srs.ExportToWkt()
            proj4text = srs.ExportToProj4()

            print('%s\t%d%s\t%s\t%d%s\t%d%s\n'
                    % (str(code), 4, auth_name, str(code), len(wkt), wkt,
                        len(proj4text), proj4text))
        except:
            pass
Exemplo n.º 8
0
def Preprocess(argv):
    names = []
    out_file = 'out.vrt'

    ulx = None
    psize_x = None
    separate = False
    pre_init = None
    a_srs = None
    a_nodata = None

    if argv is None:
        sys.exit(0)

    # Parse command line arguments.
    i = 1
    while i < len(argv):
        arg = argv[i]

        if arg == '-o':
            i = i + 1
            out_file = argv[i]

        elif arg == '-a_srs':
            i = i + 1
            a_srs = argv[i]

        elif arg == '-a_nodata':
            i = i + 1
            a_nodata = argv[i]

        elif arg == '-ul_lr':
            ulx = float(argv[i + 1])
            uly = float(argv[i + 2])
            lrx = float(argv[i + 3])
            lry = float(argv[i + 4])
            i = i + 4

        elif arg[:1] == '-':
            print 'Unrecognised command option: ', arg
            Usage()
            sys.exit(1)

        else:
            names.append(arg)

        i = i + 1

    if len(names) == 0:
        print 'No input files selected.'
        Usage()
        sys.exit(1)

    # Collect information on all the source files.
    file_infos = names_to_fileinfos(names)

    if ulx is None:
        ulx = file_infos[0].ulx
        uly = file_infos[0].uly
        lrx = file_infos[0].lrx
        lry = file_infos[0].lry

        for fi in file_infos:
            ulx = min(ulx, fi.ulx)
            uly = max(uly, fi.uly)
            lrx = max(lrx, fi.lrx)
            lry = min(lry, fi.lry)

    if psize_x is None:
        psize_x = file_infos[0].geotransform[1]
        psize_y = file_infos[0].geotransform[5]

    projection = file_infos[0].projection

    for fi in file_infos:
        pass
        # MAPTILER COMMENT
        #if fi.geotransform[1] != psize_x or fi.geotransform[5] != psize_y:
        #	print "All files must have the same scale; %s does not" \
        #		% fi.filename
        #	sys.exit(1)

        # MAPTILER COMMENT
        #if fi.geotransform[2] != 0 or fi.geotransform[4] != 0:
        #	print "No file must be rotated/skewed; %s is.\nTODO: gdalwarp -of vrt %s %s.vrt" % (fi.filename, fi.filename, fi.filename)
        #	sys.exit(1)

        #TODO: During initialization create temporary files by AutoCreateWarpedVRT for those

        #if fi.projection != projection:
        #	print "All files must be in the same projection; %s is not" \
        #		% fi.filename
        #	sys.exit(1)

    # MAPTILER COMMENT
    #geotransform = (ulx, psize_x, 0.0, uly, 0.0, psize_y)
    geotransform = file_infos[0].geotransform

    gcpprojection = file_infos[0].gcpprojection
    gcps = file_infos[0].gcps

    xsize = int(((lrx - ulx) / geotransform[1]) + 0.5)
    ysize = int(((lry - uly) / geotransform[5]) + 0.5)

    nodata = None
    if a_nodata:
        if a_nodata.find(',') != -1:
            nodata = a_nodata.split(',')
        elif a_nodata.find(' ') != -1:
            nodata = a_nodata.split(' ')
        else:
            nodata = [a_nodata] * 5  # bands + 1
        nodata = map(int, nodata)

    palette = False
    bands = file_infos[0].bands
    if file_infos[0].palette:
        palette = True
        if not (nodata or file_infos[0].nodata != [None]):
            # Palette without NODATA is expanded also to an extra alpha channel
            bands = 4
        else:
            # Palette with NODATA
            bands = 3
    palettecolors = ['Red', 'Green', 'Blue', 'Alpha']

    if a_srs:
        srs = osr.SpatialReference()
        srs.SetFromUserInput(a_srs)
        projection = srs.ExportToWkt()

    t_fh = open(out_file, 'w')

    t_fh.write('<VRTDataset rasterXSize="%i" rasterYSize="%i">\n' %
               (xsize, ysize))

    # Datasets with GCPs can't be merged without warping in advance!!!
    if len(gcps):
        t_fh.write('\t<GCPList Projection="%s">\n' %
                   gdal.EscapeString(gcpprojection, gdal.CPLES_XML))
        for gcp in gcps:
            t_fh.write(
                '\t\t<GCP Id="%s" Pixel="%.4f" Line="%.4f" X="%f" Y="%f"/>\n' %
                (gcp.Id, gcp.GCPPixel, gcp.GCPLine, gcp.GCPX, gcp.GCPY))
        t_fh.write('\t</GCPList>\n')
    else:
        t_fh.write(
            '\t<GeoTransform>%24.13f, %24.13f, %24.13f, %24.13f, %24.13f, %24.13f</GeoTransform>\n'
            % geotransform)

        if len(projection) > 0:
            t_fh.write('\t<SRS>%s</SRS>\n' %
                       gdal.EscapeString(projection, gdal.CPLES_XML))

    if nodata:
        nd = nodata
        t_fh.write(
            '\t<Metadata>\n\t\t<MDI key="NODATA_VALUES">%i %i %i</MDI>\n\t</Metadata>\n'
            % (nd[0], nd[1], nd[2]))
    if file_infos[0].nodata != [None]:
        nd = file_infos[0].nodata
        t_fh.write(
            '\t<Metadata>\n\t\t<MDI key="NODATA_VALUES">%i %i %i</MDI>\n\t</Metadata>\n'
            % (nd[0], nd[1], nd[2]))

    for band in range(1, bands + 1):
        dataType = "Byte"
        # gdal.GetDataTypeName(file_infos[0].band_types[band])

        t_fh.write('\t<VRTRasterBand dataType="%s" band="%i">\n' %
                   (dataType, band))

        if nodata:
            t_fh.write('\t\t<NoDataValue>%f</NoDataValue>\n' % nodata[band])
        elif file_infos[0].nodata != [None]:
            t_fh.write('\t\t<NoDataValue>%f</NoDataValue>\n' %
                       file_infos[0].nodata[band])
        if palette:
            t_fh.write('\t\t<ColorInterp>%s</ColorInterp>\n' %
                       palettecolors[band - 1])
        else:
            t_fh.write('\t\t<ColorInterp>%s</ColorInterp>\n' %
                       gdal.GetColorInterpretationName(
                           file_infos[0].color_interps[band]))

        for fi in file_infos:
            fi.write_source(t_fh, geotransform, xsize, ysize, band, nodata)

        t_fh.write('\t</VRTRasterBand>\n')

    t_fh.write('</VRTDataset>\n')