利用python和libredwg库解析dwg格式文件输出GeoJSON
1.python环境安装配置就不说了,先看需要的依赖
2.开源工具Libredwg
下载地址:https://ftp.gnu.org/gnu/libredwg/
git地址:https://github.com/LibreDWG/libredwg?tab=GPL-3.0-1-ov-file
安装说明在附件
from coord_convert.transform import wgs2gcj
import os
import re
import csv
import sys
import json
import time
import random
import shutil
import string
import subprocess
import numpy as np
import pandas as pd# DNV软件起始点对应的国家2000坐标系坐标值
COORDINATE_CCGS2000 = [359666.0394,3214681.411]def convert_geojson_to_gcj02(input_file):from coord_convert.transform import wgs2gcjdata = readfile(input_file, 'json')features = data.get('features')for feature in features:geometry = feature.get('geometry')if not geometry or not geometry.get('coordinates'):continuecoordinates = geometry.get('coordinates')ccgs2000 = get_ccgs2000_flag(coordinates)for index, coordinate in enumerate(coordinates):coordinate_type = type(coordinate[0])if coordinate_type == list:for item in coordinate:if ccgs2000 and item[0] < 100000 and item[1] < 100000:item[0] += COORDINATE_CCGS2000[0]item[1] += COORDINATE_CCGS2000[1]else:if ccgs2000 and coordinate[0] < 100000 and coordinate[1] < 100000:coordinate[0] += COORDINATE_CCGS2000[0]coordinate[1] += COORDINATE_CCGS2000[1]if ccgs2000:coordinates[index] = ccgs2000_to_gcj02(coordinate)else:coordinates[index] = wgs2gcj(*coordinate)features = data.get('features')indexes = list(range(len(features)))indexes.reverse()for index in indexes:feature = features[index]geometry = feature.get('geometry')if not geometry or not geometry.get('coordinates'):del features[index]continuecoordinates = geometry.get('coordinates')minval = np.min(np.array(coordinates))if minval < 10:del features[index]savefile(input_file, json.dumps(data))# 国家2000坐标系转WGS84坐标、最终转为GCJ02坐标
# EPSG:4326 -- WGS 84
# EPSG:4549 -- CGCS2000/3-degree Gauss-Kruger CM 120E
def ccgs2000_to_gcj02(coordinates):positions = []from coord_convert.transform import wgs2gcjfrom pyproj import Transformertransformer = Transformer.from_crs('EPSG:4549', 'EPSG:4326', always_xy=True)if len(coordinates) == 2 and type(coordinates[0]) in (int, float):lon, lat = transformer.transform(*coordinates)gcj_lon, gcj_lat = wgs2gcj(lon, lat)return [gcj_lon, gcj_lat]for coordinate in coordinates:if type(coordinate[0]) == list:items = []for item in coordinate:lon, lat = transformer.transform(*item)gcj_lon, gcj_lat = wgs2gcj(lon, lat)items.append([gcj_lon, gcj_lat])positions.append(items)else:lon, lat = transformer.transform(*coordinate)gcj_lon, gcj_lat = wgs2gcj(lon, lat)positions.append([gcj_lon, gcj_lat])return positions# 获取坐标值数组项最大值
def get_ccgs2000_flag(coordinates):array = np.array(coordinates)maxval = np.max(array)return maxval > 200# 读取指定路径的文件内容
def readfile(filepath, filetype='text'):content = Nonewith open(filepath, 'r') as fp:content = fp.read()if filetype == 'json':content = json.loads(content)return contentif __name__ == '__main__':if len(sys.argv) >= 3:input_file = sys.argv[1]output_file = sys.argv[2]if len(sys.argv) == 4:coordinate = sys.argv[3].split(',')COORDINATE_CCGS2000 = [float(x) for x in coordinate]cmdstr = 'dwgread %s --format GeoJSON -o %s' % (input_file, output_file)os.system(cmdstr)time.sleep(2)convert_geojson_to_gcj02(output_file)
python your_python.py your.dwg文件路径 转换成json文件的路径.json 359666.0394,3214681.411(计算的基准坐标)
使用java来执行上述python脚本命令 String[] cmd = new String[]{"python",backendConfig.getPythonFilePath(),inputFilePath,outputFilePath,baseCoordinate };
Runtime runtime = Runtime.getRuntime();
runtime.exec(cmd);