gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 18:12:35 26 4
gpt4 key购买 nike

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

<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>

最佳答案

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

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 )

关于python - 如何直接在.vrt中自动添加字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25621189/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com