geotools编写shp转sql,实现shp数据入Oracle Spatial库

概述

用到Oracle Spatial就很难避免shp文件的入库问题,虽然有shp2sdo工具,但是用起来不是很习惯,所以,本文讲述如何结合geotools实现shp2sql的转换。

效果

geotools编写shp转sql,实现shp数据入Oracle Spatial库_第1张图片
效果

实现代码

package com.lzugis.geotools;

import com.lzugis.CommonMethod;
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.simplify.TopologyPreservingSimplifier;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.factory.CommonFactoryFinder;
import org.geotools.filter.text.cql2.CQL;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.feature.type.AttributeDescriptor;
import org.opengis.feature.type.AttributeType;
import org.opengis.filter.Filter;
import org.opengis.filter.FilterFactory2;
import scala.util.parsing.combinator.testing.Str;

import java.io.File;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.lzugis.geotools.model.Fields;

public class Shp2Orcale {
    private final Map fMap = new HashMap<>();

    private SimpleFeatureSource featureSource = null;
    private String tableName = "";
    private List fields = new ArrayList<>();
    private CommonMethod cm = new CommonMethod();

    public Shp2Orcale(){
        fMap.put("string", "varchar2(64)");
        fMap.put("long", "long");
        fMap.put("double", "number");
        fMap.put("shape", "\"MDSYS\".\"SDO_GEOMETRY\"");
    }

    public void readShape(String shpfile){
        try {
            File file = new File(shpfile);
            ShapefileDataStore shpDataStore = null;

            shpDataStore = new ShapefileDataStore(file.toURL());
            //设置编码
            Charset charset = Charset.forName("GBK");
            shpDataStore.setCharset(charset);
            tableName = shpDataStore.getTypeNames()[0];
            featureSource =  shpDataStore.getFeatureSource (tableName);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public void getShpFields(){
        SimpleFeatureType schema = featureSource.getSchema();
        List attrs= schema.getAttributeDescriptors();
        for(int i=0;i cls = attr.getType().getBinding();
            String clsName = cls.getName();
            clsName = clsName.substring(clsName.lastIndexOf(".")+1).toLowerCase();
            Fields field = new Fields(attr.getLocalName(), clsName);
            fields.add(field);
        }
    }

    public void createTableSql(String sqlpath){
        cm.append2File(sqlpath, "--1.drop table\r\nDROP TABLE "+tableName+";");
        cm.append2File(sqlpath, "\r\n--2.create table\r\n");
        StringBuffer sql = new StringBuffer();
        sql.append("CREATE TABLE "+tableName+" (");
        for(int i=0, size = fields.size();i

说明:
1、对于比较复杂的线或面会出现sql太长的问题,文章里面加了一行简化的代码;


技术博客
CSDN:http://blog.csdn.NET/gisshixisheng
在线教程
https://edu.csdn.net/course/detail/799
https://edu.csdn.net/course/detail/7471
联系方式

类型 内容
qq 1004740957
公众号 lzugis15
e-mail [email protected]
webgis群 452117357
Android群 337469080
GIS数据可视化群 458292378

“GIS讲堂”知识星球开通了,在星球,我将提供一对一的问答服务,你问我答,期待与你相见。


geotools编写shp转sql,实现shp数据入Oracle Spatial库_第2张图片
知识星球二维码
geotools编写shp转sql,实现shp数据入Oracle Spatial库_第3张图片
LZUGIS

你可能感兴趣的:(geotools编写shp转sql,实现shp数据入Oracle Spatial库)