JAVA实现Shapefile等转DXF格式

      用geotools容易将shapefile、geojson、kml等地理信息数据解析成它识别的FeatureCollection对象,然后在进行其他的数据操作。dxf文件是AutoCAD的数据交换文件,不带坐标系定义。有时候需要将以上的这些数据转成dxf文件,这时需要借助gdal环境和java本地方法接口写入,读取dxf也能实现(本文不介绍)。本文用基于java语言的geotools和gdal本地方法接口来处理FeatureCollection对象。

1.gdal环境

  • 安装

Linux:

root@shanks:~# apt-get install gdal-bin libgdal-dev libgdal-java
注意安装的版本,本文示例是在Linux环境下实现。
Windows:

下载地址http://www.gdal.org/usergroup0.html,下载安装相应的安装包。

  • JNI包
      gdal安装后提供本地方法给java调用,对应的版本JNI接口依赖添加(本例使用的是2.1.0):
maven:

    org.gdal
    gdal
    2.1.0
gradle:
org.gdal:gdal:2.1.0
      有时不能准确的找到.dll或者.so文件,需记住库文件所在的路径。本例子库文件路径是在“/usr/lib/jni”下,目录下有文件:
root@shanks:/usr/lib/jni# ls
libgdalconstjni.so  libgdalconstjni.so.20  libgdaljni.so  libgdaljni.so.20  libogrjni.so  libogrjni.so.20  libosrjni.so  libosrjni.so.20
2.数据操作

  • 完整的代码
Demo.java
package shanks.demo;

import org.gdal.gdal.gdal;
import org.gdal.ogr.*;
import org.gdal.osr.SpatialReference;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.feature.FeatureCollection;
import org.geotools.referencing.CRS;
import org.opengis.feature.simple.SimpleFeature;
import java.io.File;

public class Demo {

    public static void main(String[] args) {
        try {
            // 注册驱动
            ogr.RegisterAll();
            // 支持中文路径
            gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8","YES");
            // 属性表字段支持中文
            gdal.SetConfigOption("SHAPE_ENCODING","CP936");

            String in = "/home/shanks/shp/test_shp/t.shp";  // 输入文件路径
            String out = "/home/shanks/shp/test_shp/dxf/dxf.dxf";  // 输出文件路径
            File shp_file = new File(in);

            // 读shapefile
            ShapefileDataStore dataStore = new ShapefileDataStore(shp_file.toURI().toURL());
            // 得到FeatureCollection对象
            FeatureCollection featureCollection = dataStore.getFeatureSource().getFeatures();
            //坐标系
            String srs = CRS.lookupIdentifier(featureCollection.getSchema().getCoordinateReferenceSystem(),true);
            int srid = Integer.valueOf(srs.split(":")[1]);

            // 将要输出的类型,这里是导出DXF文件
            Driver driver = ogr.GetDriverByName("DXF");
            if(null == driver)
                throw new NullPointerException("获取DXF驱动失败!");

            DataSource dataSource = driver.CreateDataSource(out);
            if(null == dataSource)
                throw new NullPointerException("DataSource创建失败!");

            // gdal坐标系
            SpatialReference spatialReference = new SpatialReference();
            spatialReference.ImportFromEPSG(srid);

            final String layer0 = "Layer0";
            // 创建一个Layer。dxf中可以创建多个Layer
            Layer layer = dataSource.CreateLayer(layer0,spatialReference);
            if(null == layer)
                throw new NullPointerException("Layer创建失败!");

            // dxf的属性字段集合。固定的某些字段:
            // "ExtendedEntity","SubClasses","Text","EntityHandle","Linetype","Layer"等
            FeatureDefn featureDefn = layer.GetLayerDefn();
            // 查看固定属性
            for (int i=0;i properties = simpleFeature.getProperties();

                // 写入"Layer"字段值
                feature.SetField("Layer", layer0);
                // 几何信息,WKT
                Geometry geometry = ogr.CreateGeometryFromWkt(simpleFeature.getDefaultGeometry().toString());
                feature.SetGeometry(geometry);
                // layer添加feature
                layer.CreateFeature(feature);
            }

            // 关闭迭代
            iterator.close();
            // 关闭对象连接
            dataSource.delete();

            // 以下几行代码判断Feature的写入数量
            DataSource source = ogr.Open(out,0);
            boolean code = featureCollection.size() == source.GetLayer(0).GetFeatureCount();
            System.out.println("\n判断写入前后Feature数量是否相等: " + code);
            source.delete();

        } catch (Throwable e){
            e.printStackTrace();
        }
    }
}

运行时可以带虚拟机参数:

-Djava.library.path="/usr/lib/jni"
  • 输出结果:
Layer: 4
SubClasses: 4
ExtendedEntity: 4
Linetype: 4
EntityHandle: 4
Text: 4
BlockName: 4
BlockScale: 3
BlockAngle: 2

判断写入前后Feature数量是否相等: true
      具体流程见代码中的注释,执行无误后得到文件/home/shanks/shp/test_shp/dxf/dxf.dxf,可在AutoCAD、QGIS、ArcGIS中打开。可见只需用geotools框架将geojson、kml、gml3等数据解析成FeatureCollection中间对象,很容易将其转为dxf文件。

你可能感兴趣的:(java,geotools,gdal,gis,java,geotools,gdal)