dxf2gmlcatastro.py 文件源码

python
阅读 14 收藏 0 点赞 0 评论 0

项目:dxf2gmlcatastro 作者: sigdeletras 项目源码 文件源码
def crea_gml(dxf_origen_file, gml_salida_file, src):
    """ Transforma la información de la geometría de un archivo DXF al estándar de Catastro en formato GML.

    :dxf_origen_file:   Dirección del archivo en formato DXF con la geometría de origen
    :gml_salida_file:   Dirección del archivo en formato GML a sobreescribir con el resultado
    :src:               Sistema de Refencia de Coordendas del DXF. Según cógigos  EPSG
    """
    # Accede mediante GDAL al archivo DXF
    driver = ogr.GetDriverByName('DXF')
    data_source = driver.Open(dxf_origen_file, 0)
    layer = data_source.GetLayer()

    if src not in SRC_DICT:     # Comprueba que el SRC es correcto
        print('ERROR: El código SRC ({}) indicado es incorrecto.'.format(src))
        print('Los SRC permitidos son 25828, 25829, 25830 o 25831')

        sys.exit()

    print('Archivo GML de salida: {}'.format(gml_salida_file))
    print('Código EPSG seleccionado: {}\n'.format(src))

    with open(gml_salida_file, 'w') as filegml:
        filegml.writelines(PLANTILLA_1)

        print('El archivo {} contiene {} geometría.'.format(dxf_origen_file, layer.GetFeatureCount()))

        for feature in layer:
            geom = feature.GetGeometryRef()

            area = geom.Area()
            print('El área del polígono es {:.4f} m2.'.format(area))

            filegml.writelines(str(area))       # Añade el área al GML

            perimetro = geom.GetGeometryRef(0)

            print('\nTotal de vértices del polígono: {}'.format(perimetro.GetPointCount()))
            print('Listado de coordenadas de los vértices:\nid,x,y')

            filegml.writelines(PLANTILLA_2.format(src=src))

            for i in range(0, perimetro.GetPointCount()):
                pt = perimetro.GetPoint(i)
                coordlist = [str(pt[0]), ' ', str(pt[1]), '\n']

                filegml.writelines(coordlist)       # Añade listado de coordenadas X e Y al GML

                print('{},{:.4f},{:.4f}'.format(i, pt[0], pt[1]))

        filegml.writelines(PLANTILLA_3)     # Añade XML
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号