GDAL-Python将s57数据转换为GeoJSON

发布时间 2023-07-24 23:35:01作者: 槑孒

from osgeo import ogr
import subprocess
import glob
import os

OGR_S57_OPTIONS = "SPLIT_MULTIPOINT=ON,ADD_SOUNDG_DEPTH=ON"

def GetAllS57Repertory():
    S57Path = []
    for _file in glob.glob(('{0}{1}*.000').format(path, os.sep)):
        S57Path.append(_file)
    return S57Path


def convert():
    # geojson_layers = []
    for _file in GetAllS57Repertory():
        input_file = ogr.Open(_file, 0)
        if not os.path.exists(path+"/geojson/" + os.path.splitext(os.path.basename(_file))[0]):
            os.makedirs(path+"/geojson/" + os.path.splitext(os.path.basename(_file))[0])

        # Get the layers in the file
        enc_layers = []
        for featsClass_idx in range(input_file.GetLayerCount()):
            featsClass = input_file.GetLayerByIndex(featsClass_idx)
            enc_layer = featsClass.GetName()
            enc_layers.append(enc_layer)

        # Convert them into geojson
        for enc_layer in enc_layers:
            # print(f"Converting {enc_layer} to geojson...")
            geojson_layer = f"{path}/geojson/{os.path.splitext(os.path.basename(_file))[0]}/{enc_layer}.geojson"

            # Use ogr2ogr to extract geojson from the enc file
            opt = 'RETURN_PRIMITIVES=OFF, RETURN_LINKAGES=OFF, LNAM_REFS=ON, UPDATES=APPLY, SPLIT_MULTIPOINT=ON,RECODE_BY_DSSI=ON: ADD_SOUNDG_DEPTH = ON'
            subprocess.call(
                # RETURN_PRIMITIVES=OFF, RETURN_LINKAGES=OFF, LNAM_REFS=ON, UPDATES=APPLY, SPLIT_MULTIPOINT=ON,RECODE_BY_DSSI=ON: ADD_SOUNDG_DEPTH = ON
                # f"OGR_S57_OPTIONS={OGR_S57_OPTIONS} ogr2ogr -f GeoJSON -lco 'ENCODING=ascii' -t_srs EPSG:4326 {geojson_layer} {_file} {enc_layer}",
                # f"OGR_S57_OPTIONS={OGR_S57_OPTIONS} ogr2ogr --config SHAPE_ENCODING -f GeoJSON -t_srs EPSG:4326 {geojson_layer} {_file} {enc_layer}",
                f"ogr2ogr -overwrite -f GeoJSON {geojson_layer} {_file} {enc_layer}",
                shell=True,
            )


if __name__ == "__main__":
    path = 'D:/DATA/charts'
    convert()