GeoTools——新建shapefile文件

目錄

一、引言

二、代碼操作

1、服務端

2、返回數據

3、客戶端

三、總結


 

 

一、引言

 

本文講解了如何讀取shp數據,如何新建shp數據,並將新建的shp數據返回到客戶端進行顯示。

 

 

二、代碼操作

 

1、服務端

list對象是爲了最後將新建的shp數據返回,pointgbk數據是源文件,pointgbkbuffer是新建文件。

    @RequestMapping("/buffer")
    @ResponseBody
    public Object buffer()
    {
        List<Map<String,Object>>list=new ArrayList<>();
        long start = System.currentTimeMillis();

        String shpfile = this.getClass().getResource("/").getFile()+"file/pointgbk.shp";
        String buffile = this.getClass().getResource("/").getFile()+"file/pointgbkbuffer.shp";

        try{
            //讀取shp文件
            File file = new File(shpfile);
            ShapefileDataStore shpDataStore = null;
            shpDataStore = new ShapefileDataStore(file.toURL());
            //設置編碼
            Charset charset = Charset.forName("GBK");
            shpDataStore.setCharset(charset);
            String typeName = shpDataStore.getTypeNames()[0];
            SimpleFeatureSource featureSource = null;
            featureSource =  shpDataStore.getFeatureSource (typeName);
            SimpleFeatureCollection result = featureSource.getFeatures();
            SimpleFeatureIterator itertor = result.features();

            //創建shape文件對象
            File fileBuf = new File(buffile);
            Map<String, Serializable> params = new HashMap<String, Serializable>();
            params.put( ShapefileDataStoreFactory.URLP.key, fileBuf.toURI().toURL() );
            ShapefileDataStore ds = (ShapefileDataStore) new ShapefileDataStoreFactory().createNewDataStore(params);
            //獲取原shp文件字段名
            SimpleFeatureType sft = featureSource.getSchema();
            List<AttributeDescriptor> attrs = sft.getAttributeDescriptors();

            //定義圖形信息和屬性信息
            SimpleFeatureTypeBuilder tb = new SimpleFeatureTypeBuilder();
            //tb.setCRS(DefaultGeographicCRS.WGS84);
            tb.setName("shapefile");
            for(int i=0;i<attrs.size();i++){
                AttributeDescriptor attr = attrs.get(i);
                String fieldName = attr.getName().toString();
                if(fieldName=="the_geom"){
                    tb.add(fieldName, Polygon.class);
                }
                else{
                    tb.add(fieldName, String.class);
                }
            }
            ds.createSchema(tb.buildFeatureType());
            //設置編碼
            ds.setCharset(charset);
            //設置Writer,遍歷原feature,添加到新feature
            FeatureWriter<SimpleFeatureType, SimpleFeature> writer = ds.getFeatureWriter(ds.getTypeNames()[0], Transaction.AUTO_COMMIT);

            while (itertor.hasNext())
            {
                SimpleFeature feature = itertor.next();
                SimpleFeature featureBuf = writer.next();
                featureBuf.setAttributes(feature.getAttributes());
                //Object temp=feature.getAttribute("the_geom");
                //更新the_geom字段,改爲buffer後的polygon
                Geometry geo = (Geometry)feature.getAttribute("the_geom");
                Geometry geoBuffer = geo.buffer(1.5);
                featureBuf.setAttribute("the_geom", geoBuffer);
                Map<String, Object> map=new HashMap<>();
                map.put("wkt",geoBuffer.toString());
                list.add(map);
            }
            writer.write();
            writer.close();
            itertor.close();
        }
        catch(Exception e){
            e.printStackTrace();
        }
        System.out.println("共耗時"+(System.currentTimeMillis() - start)+"ms");

        return list;
    }

 

2、返回數據

[{"wkt":"POLYGON ((508930.905 299800.519, 508930.8761779206 299800.22636451694, 508930.79081929877 299799.94497485145, 508930.6522044185 299799.6856446504, 508930.4656601718 299799.4583398282, 508930.2383553496 299799.2717955815, 508929.97902514855 299799.13318070123, 508929.69763548305 299799.0478220794, 508929.405 299799.019, 508929.112364517 299799.0478220794, 508928.8309748515 299799.13318070123, 508928.5716446505 299799.2717955815, 508928.34433982824 299799.4583398282, 508928.15779558156 299799.6856446504, 508928.0191807013 299799.94497485145, 508927.93382207945 299800.22636451694, 508927.905 299800.519, 508927.93382207945 299800.811635483, 508928.0191807013 299801.0930251485, 508928.15779558156 299801.3523553495, 508928.34433982824 299801.57966017176, 508928.5716446505 299801.76620441844, 508928.8309748515 299801.9048192987, 508929.112364517 299801.99017792055, 508929.405 299802.019, 508929.69763548305 299801.99017792055, 508929.97902514855 299801.9048192987, 508930.2383553496 299801.76620441844, 508930.4656601718 299801.57966017176, 508930.6522044185 299801.3523553495, 508930.79081929877 299801.0930251485, 508930.8761779206 299800.811635483, 508930.905 299800.519))"},{"wkt":"POLYGON ((508911.3539904643 299887.2300114852, 508911.3251683849 299886.9373760022, 508911.23980976304 299886.6559863367, 508911.10119488277 299886.3966561357, 508910.9146506361 299886.16935131344, 508910.68734581384 299885.98280706676, 508910.4280156128 299885.8441921865, 508910.1466259473 299885.75883356464, 508909.8539904643 299885.7300114852, 508909.56135498127 299885.75883356464, 508909.27996531577 299885.8441921865, 508909.02063511475 299885.98280706676, 508908.7933302925 299886.16935131344, 508908.6067860458 299886.3966561357, 508908.46817116556 299886.6559863367, 508908.3828125437 299886.9373760022, 508908.3539904643 299887.2300114852, 508908.3828125437 299887.52264696825, 508908.46817116556 299887.80403663375, 508908.6067860458 299888.0633668348, 508908.7933302925 299888.290671657, 508909.02063511475 299888.4772159037, 508909.27996531577 299888.61583078397, 508909.56135498127 299888.7011894058, 508909.8539904643 299888.7300114852, 508910.1466259473 299888.7011894058, 508910.4280156128 299888.61583078397, 508910.68734581384 299888.4772159037, 508910.9146506361 299888.290671657, 508911.10119488277 299888.0633668348, 508911.23980976304 299887.80403663375, 508911.3251683849 299887.52264696825, 508911.3539904643 299887.2300114852))"},……]

 

3、客戶端

使用openlayers接受並顯示

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="../js/jquery-3.3.1.js"></script>
    <script src="../js/ol.js"></script>
    <link rel="stylesheet" href="../js/ol.css">
</head>
<body>
<div id="map" style="width: 100%"></div>
<script>


    var wktFormat = new ol.format.WKT();
    $.ajax({
        type: 'POST',
        url: "http://localhost:8080/testmaven/shp/buffer",
        data: {
            layer: "capital"
        },
        success: function (data) {
            //data = eval("(" + data + ")");
            console.log(data);
            for (var i = 0, dl = data.length; i < dl; i++) {
                var d = data[i];
                var geometry = wktFormat.readFeature(d.wkt);
                polygonVectorSource.addFeature(geometry);
            }
            //pointVectorSource.addFeature(feature);
        }
    });
    //面樣式
    var fill = new ol.style.Fill({
        color: '#dd942e'
    });
    var stroke = new ol.style.Stroke({
        color: '#cc1000',
        width: 1.25
    });
    var stylePolygon = [
        new ol.style.Style({
            image: new ol.style.Circle({
                fill: new ol.style.Fill({
                    color: 'rgba(255,0,255,0.4)'
                }),
                stroke: new ol.style.Stroke({
                    color: '#cc3540',
                    width: 1.25
                }),
                radius: 5
            }),
            fill: fill,
            stroke: stroke
        })
    ];
    var polygonVectorSource = new ol.source.Vector({
        //features:[feature1,feature2]
    });
    //面圖層
    var polygonVectorLayer = new ol.layer.Vector({
        title:"面",
        source: polygonVectorSource,
        //style: styleFunction,
        style:stylePolygon,
        renderMode:'image'
    });

    /*視圖*/
    var view=new ol.View({
        // 設置地圖中心
        center: ol.proj.transform([508881,299705],"EPSG:3857","EPSG:3857"),
        zoom: 19,
        projection: 'EPSG:3857'
    });
    // map
    var map = new ol.Map({
        layers: [
            polygonVectorLayer
        ],
        view:view,
        target: 'map'
    });

</script>
</body>
</html>

 

 

三、總結

 

  • 讀取shp文件;

 

  • 獲取要素信息;

 

  • 新建shp文件;

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章