如何直接在.vrt中自动添加字段?

时间:2014-09-02 10:27:02

标签: python raster gdal

我有一个VRT文件,我想自动添加一个颜色表。 我想在(在python中)将字段ColorTable添加到文件中的正确位置。 要明确:我有以下几行:

<VRTDataset rasterXSize="40320" rasterYSize="14560">
  <SRS>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"]]</SRS>
  <GeoTransform> -1.8000446428500001e+02,  8.9285700000000003e-03,  0.0000000000000000e+00,  6.5004464284999997e+01,  0.0000000000000000e+00, -8.9285700000000003e-03</GeoTransform>
  <Metadata>
    <MDI key="AREA_OR_POINT">Area</MDI>
  </Metadata>
  <VRTRasterBand dataType="Byte" band="1">
    <Metadata>
      <MDI key="STATISTICS_MAXIMUM">4</MDI>
      <MDI key="STATISTICS_MEAN">1.9570865680717</MDI>
      <MDI key="STATISTICS_MINIMUM">0</MDI>
      <MDI key="STATISTICS_STDDEV">1.9845596274822</MDI>
    </Metadata>
    <NoDataValue>2.55000000000000E+02</NoDataValue>
    <ColorInterp>Gray</ColorInterp>
    <SimpleSource>
      <SourceFilename relativeToVRT="1">DISCR___MC10GWW_20131221_1KM_WB_MODIS__MC10GWW_20131221_1KM_WB_V23.tif</SourceFilename>
      <SourceBand>1</SourceBand>
      <SourceProperties RasterXSize="40320" RasterYSize="14560" DataType="Byte" BlockXSize="40320" BlockYSize="1" />
      <SrcRect xOff="0" yOff="0" xSize="40320" ySize="14560" />
      <DstRect xOff="0" yOff="0" xSize="40320" ySize="14560" />
    </SimpleSource>
  </VRTRasterBand>
</VRTDataset>

我希望得到以下内容:

    <VRTDataset rasterXSize="40320" rasterYSize="14560">
      <SRS>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"]]</SRS>
      <GeoTransform> -1.8000446428500001e+02,  8.9285700000000003e-03,  0.0000000000000000e+00,  6.5004464284999997e+01,  0.0000000000000000e+00, -8.9285700000000003e-03</GeoTransform>
      <Metadata>
        <MDI key="AREA_OR_POINT">Area</MDI>
      </Metadata>
      <VRTRasterBand dataType="Byte" band="1">
        <Metadata>
          <MDI key="STATISTICS_MAXIMUM">4</MDI>
          <MDI key="STATISTICS_MEAN">1.9570865680717</MDI>
          <MDI key="STATISTICS_MINIMUM">0</MDI>
          <MDI key="STATISTICS_STDDEV">1.9845596274822</MDI>
        </Metadata>
        <NoDataValue>2.55000000000000E+02</NoDataValue>
 <ColorInterp>Palette</ColorInterp>
     <ColorTable>
      <Entry c1="255" c2="255" c3="255" c4="255"/>
      <Entry c1="0" c2="0" c3="255" c4="255"/>
       <Entry c1="0" c2="0" c3="0" c4="255"/>
       <Entry c1="0" c2="0" c3="0" c4="255"/>
       <Entry c1="0" c2="0" c3="0" c4="255"/>
       <Entry c1="0" c2="0" c3="0" c4="255"/>
       <Entry c1="0" c2="0" c3="0" c4="255"/>
       <Entry c1="0" c2="0" c3="0" c4="255"/>
       <Entry c1="0" c2="0" c3="0" c4="255"/>
       <Entry c1="0" c2="0" c3="0" c4="255"/>
       <Entry c1="223" c2="115" c3="255" c4="255"/>
       <Entry c1="255" c2="255" c3="0" c4="255"/>
       <Entry c1="0" c2="0" c3="0" c4="255"/>
       <Entry c1="0" c2="0" c3="0" c4="255"/>
    </ColorTable>
 <SimpleSource>
          <SourceFilename relativeToVRT="1">DISCR___MC10GWW_20131221_1KM_WB_MODIS__MC10GWW_20131221_1KM_WB_V23.tif</SourceFilename>
          <SourceBand>1</SourceBand>
          <SourceProperties RasterXSize="40320" RasterYSize="14560" DataType="Byte" BlockXSize="40320" BlockYSize="1" />
          <SrcRect xOff="0" yOff="0" xSize="40320" ySize="14560" />
          <DstRect xOff="0" yOff="0" xSize="40320" ySize="14560" />
        </SimpleSource>
      </VRTRasterBand>
    </VRTDataset>

2 个答案:

答案 0 :(得分:1)

我终于找到了具有以下功能的解决方案:

def Modify( filename , Color , Category ):
        dataset = osgeo.gdal.Open( filename , GA_Update )
        if dataset is None:
            print 'ERROR: gdal.Open( %(filename)s )' % var
            sys.exit( 1 )
        band = dataset.GetRasterBand( 1 )
        band.SetRasterColorInterpretation( GCI_PaletteIndex )
        band.SetRasterColorTable( Color )
        band.SetRasterCategoryNames( Category )

答案 1 :(得分:0)

您是否在此链接http://www.gdal.org/gdal_vrttut.html上尝试过VRT格式教程?或者查看此链接http://www.gdal.org/classVRTRasterBand.html上的VRT Raster Band文档?我认为你应该可以使用AddColorTable