首页 > 编程语言 > 详细

Java调用开源GDAL解析dxf成shp,再调用开源GeoTools解析shp文件

时间:2021-08-03 18:42:49      阅读:122      评论:0      收藏:0      [点我收藏+]
  • 需求

    最近项目要做国产化,有一个导入CAD的功能需要修改。要换成开源的或者国产的技术去解析CAD。DWG格式的,懂的都懂,开源的不可能解析,国产收费的那些CAD公司,个人认为也是买的AutoCAD的接口解析的。所以我们只能改成解析CAD的另一种文件格式:DXF文件。

  • 方案

    解析dxf用开源的GDAL,调用GDAL驱动把dxf转成shp文件,然后再用开源的GeoTools去解析shp文件。

    GDAL的依赖下载地址:https://www.gisinternals.com/release.php

    GeoTools包下载地址(也可以用maven):https://sourceforge.net/projects/geotools/files/

  • 代码

  

dxf转shp代码片段:

import org.gdal.gdal.gdal;
import org.gdal.ogr.DataSource;
import org.gdal.ogr.Driver;
import org.gdal.ogr.Layer;
import org.gdal.ogr.ogr;

public String getDxfData() throws Exception{
            
            //dxf文件路径
            String filePath ="C:\\WorkSpace\\rect_field_demo.dxf";
            // 注册所有的驱动
            ogr.RegisterAll();
            gdal.SetConfigOption("DXF_ENCODING","UTF-8");
            gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8","YES");//支持中文路径
            gdal.SetConfigOption("SHAPE_ENCODING","CP936");//属性表字段支持中文
            DataSource ds = ogr.Open(filePath,1);
            if (ds == null)
            {
                System.out.println("打开文件失败!" );
                return null;
            }
            System.out.println("打开文件成功!" );
            Layer oLayer = ds.GetLayerByIndex(0);
            if(oLayer == null){
                System.out.println("获取失败");
                return null;
            }
            oLayer.ResetReading();
            Driver dv = ogr.GetDriverByName("ESRI Shapefile"); //调用驱动转shp
            String extfile = "C:\\WorkSpace\\rect_field_demo.shp";
            DataSource dataSource = dv.CopyDataSource(ds, extfile);//创建shp文件并写入内容
            dataSource.delete();    //释放与数据源对象关联的本机资源并关闭文件(这句非常重要,如果没有关闭文件,那么下面的解析shp就解析不了)
            String geometry = getShpData("C:\\WorkSpace\\rect_field_demo.shp",coordInfo);

            return geometry;

        }
解析shp文件

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.*;

import org.geotools.data.shapefile.ShapefileDataStore;
import org.opengis.feature.Property;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.geotools.data.FeatureSource;
import org.geotools.feature.FeatureCollection;
import org.geotools.feature.FeatureIterator;

public static String getShpData(String filePath) throws Exception {
        File file = new File(filePath);
        long fileSize = file.length();
        List<Map<String,Object>> list = new ArrayList<Map<String, Object>>();
        ShapefileDataStore shpDataStore = new ShapefileDataStore(file.toURL());
        shpDataStore.setStringCharset(Charset.forName("GBK"));
        String typeName = shpDataStore.getTypeNames()[0];
        FeatureSource<SimpleFeatureType, SimpleFeature> featureSource = null;
        featureSource = (FeatureSource<SimpleFeatureType, SimpleFeature>)shpDataStore.getFeatureSource(typeName);//这里我看了很多人的博客都没有指定类(FeatureSource<SimpleFeatureType, SimpleFeature>),难道不会报错吗,反正我的会
        FeatureCollection<SimpleFeatureType, SimpleFeature> result = featureSource.getFeatures();

        FeatureIterator<SimpleFeature> itertor = result.features();
        while (itertor.hasNext())
        {
            Map<String,Object> data  = new HashMap<String, Object>();
            SimpleFeature feature = itertor.next();
            Collection<Property> p = feature.getProperties();
            Iterator<Property> it = p.iterator();
            while(it.hasNext()) {
                Property pro = it.next();
                String field = pro.getName().toString();
                String value = pro.getValue().toString();
                field = field.equals("the_geom")?"wkt":field;
                data.put(field, value);
            }
            list.add(data);
        }
        JSONArray jsonarray = JSONArray.fromObject(list);
        return jsonarray.toString();
    }

 

 

ps:Java大学的时候学过,之后就没写过了,这里基本上全是抄的,哈哈哈哈,抄得不好见谅!忘记抄的谁的了,就不注明了。

Java调用开源GDAL解析dxf成shp,再调用开源GeoTools解析shp文件

原文:https://www.cnblogs.com/webgis-ling/p/15095192.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!