agrobot_base/Python/map-convert/map-convert-dbf.py

50 lines
1.6 KiB
Python

import geopandas as gpd
import json
# Carregar o arquivo shapefile contendo a geometria
input_shapefile = 'LineFeature.shp'
data_geometry = gpd.read_file(input_shapefile)
# Carregar o arquivo DBF com informações adicionais
input_dbf = 'LineFeature.dbf'
data_dbf = gpd.read_file(input_dbf)
# Mesclar as informações do arquivo DBF com o dataframe de geometria
data_merged = data_geometry.merge(data_dbf, left_index=True, right_index=True)
# Escolher uma coluna de geometria
geometry_column = 'geometry_y'
# Criar um novo GeoDataFrame com uma única coluna de geometria
data_geodataframe = gpd.GeoDataFrame(data_merged[[geometry_column]], geometry=geometry_column)
# Criar o campo 'properties' e preencher com as colunas do DataFrame original
data_geodataframe['properties'] = data_merged.apply(lambda row: {
'Id': row['Id_x'],
'Name': row['Name_x'],
'Length': row['Length_x'],
'Dist1': row['Dist1_x'],
'Dist2': row['Dist2_x']
}, axis=1)
# Caminho para salvar o arquivo GEOJson com as informações adicionais
output_geojson = 'dbGEOJson.json'
# Salvar os dados no formato GEOJson
data_geodataframe.to_file(output_geojson, driver='GeoJSON')
# Abrir o arquivo GeoJSON
with open(output_geojson, 'r') as file:
data = json.load(file)
# Iterar sobre as features no arquivo GeoJSON
for feature in data['features']:
# Obter o dicionário 'properties' dentro de 'properties'
properties = feature['properties']['properties']
# Remover a camada extra de 'properties'
feature['properties'] = properties
# Salvar o arquivo GeoJSON atualizado
with open(output_geojson, 'w') as file:
json.dump(data, file)