欧美free性护士vide0shd,老熟女,一区二区三区,久久久久夜夜夜精品国产,久久久久久综合网天天,欧美成人护士h版

目錄

geotools讀取shape文件和創(chuàng)建shape文件具體實現(xiàn)代碼

最近兩周一直在看geotools,經(jīng)過兩周的學(xué)習(xí)對geotools有了一個大概的認識,能做做一些基本的業(yè)務(wù)分析,說到geotools就讓我想到arcgis的AE,記得去年我研究AE整整用了兩個月的時間才有一點認識,arcgis系列的開發(fā)體系完整而且龐大,學(xué)習(xí)成本較高,在這里主要寫一篇關(guān)于shape文件的讀取和創(chuàng)建,關(guān)于里面一些包的引用,對代碼做一些簡單的注解。這里我用的geotools版本是20。說個坑關(guān)于import org.locationtech.jts.geom.Geometry;和import com.vividsolutions.jts.geom.Geometry;前者是新版本所采用的JTS,后者是老版本采用的JTS,你要采用像geotools的20版本,用前者,用geotool的18版本以前的用后者,否則會報錯。

一、shape文件讀取

1、主要的引用

import org.geotools.data.shapefile.ShapefileDataStore;

import org.geotools.data.shapefile.ShapefileDataStoreFactory;

import org.geotools.data.simple.SimpleFeatureCollection;

import org.geotools.data.simple.SimpleFeatureIterator;

import org.geotools.data.simple.SimpleFeatureSource;

2、獲取feature代碼

            //shape文件路徑

            String shpfile = "C:/Users/lenovo/Desktop/shape/locations1.shp";

            File file = new File(shpfile);

            //聲明一個存儲空間

            ShapefileDataStore shpDataStore = null;

            //將文件讀取到存儲空間

            shpDataStore = new ShapefileDataStore(file.toURL());

            //設(shè)置編碼,防止中文亂碼

            Charset charset = Charset.forName("GBK");

            shpDataStore.setCharset(charset);

            //獲取圖層名稱

            String typeName = shpDataStore.getTypeNames()[0];

            System.out.println(typeName);

            SimpleFeatureSource featureSource = null;

            //根據(jù)圖層名稱來獲取要素的source

            featureSource = shpDataStore.getFeatureSource (typeName);

            //獲取要素集

            SimpleFeatureCollection result = featureSource.getFeatures();

            //獲取要素集合,方便進行迭代讀取每一個要素

            SimpleFeatureIterator itertor = result.features(); 

3、關(guān)于要素集合迭代代碼示例

             //循環(huán)讀取feature,itertor.hasNext()表示游標下一個是否有數(shù)據(jù),有返回ture,否則為false               

            while (itertor.hasNext())

            {

                //獲取每一個要素

                SimpleFeature feature = itertor.next();

            } 

二、創(chuàng)建shape文件

1、主要的引用

import org.opengis.feature.simple.SimpleFeature;

import org.opengis.feature.simple.SimpleFeatureType;

import org.opengis.feature.type.AttributeDescriptor;

import org.geotools.data.FeatureWriter;

import org.geotools.data.Transaction;

import org.locationtech.jts.geom.Coordinate;

import org.locationtech.jts.geom.Geometry;

import org.locationtech.jts.geom.Point;

2、創(chuàng)建shape文件核心代碼

        //設(shè)置要素的字段名稱及其類型

        final SimpleFeatureType TYPE =

                DataUtilities.createType(

                        "Location",

                        "the_geom:Point:srid=4326,"// geometry屬性設(shè)置

                                + "name:String,"// 一個字符串屬性                               

                                + "number:Integer" // 數(shù)字屬性設(shè)置

                        );

                //創(chuàng)建要素集合

        List<SimpleFeature> features = new ArrayList<>();

                //創(chuàng)建要素模板

        SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(TYPE);

                double latitude = Double.parseDouble(tokens[0]);

        double longitude = Double.parseDouble(tokens[1]);

        String name = tokens[2].trim();

        int number = Integer.parseInt(tokens[3].trim());

        //創(chuàng)建一個點geometry

        Point point = geometryFactory.createPoint(new Coordinate(longitude, latitude));

        //添加geometry屬性

        featureBuilder.add(point);

        /添加name屬性

        featureBuilder.add(name);

        //添加number屬性

        featureBuilder.add(number);

        //構(gòu)建要素

        SimpleFeature feature = featureBuilder.buildFeature(null);

        //將要素添加到要素幾何中

         features.add(feature);

                        String shpfile = "C:/Users/lenovo/Desktop/shape/New.shp";

        File newFile = getNewShapeFile(file);

        //創(chuàng)建shapefileDataStore工廠

        ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();

        //參數(shù)設(shè)置

        Map<String, Serializable> params = new HashMap<>();

        params.put("url", newFile.toURI().toURL());

        params.put("create spatial index", Boolean.TRUE);

        //根據(jù)關(guān)鍵字創(chuàng)建shapefileDataStore

        ShapefileDataStore newDataStore =(ShapefileDataStore) dataStoreFactory.createNewDataStore(params);

               //設(shè)置編碼,防止中文亂碼

               Charset charset = Charset.forName("GBK");

               newDataStore.setCharset(charset);

       //創(chuàng)建文件描述內(nèi)容

       newDataStore.createSchema(TYPE);

               //設(shè)置Writer,并設(shè)置為自動提交

              FeatureWriter<SimpleFeatureType, SimpleFeature> writer = ds.getFeatureWriter(ds.getTypeNames()[0], Transaction.AUTO_COMMIT);

             //循環(huán)寫入要素

            while (itertor.hasNext())

            {

            //獲取要寫入的要素

                SimpleFeature feature = itertor.next();

                //將要寫入位置

                SimpleFeature featureBuf = writer.next();

                //設(shè)置寫入要素所有屬性

                featureBuf.setAttributes(feature.getAttributes());

                //獲取the_geom屬性的值

                Geometry geo =(Geometry) feature.getAttribute("the_geom");

                Geometry geoBuffer = geoR.calBuffer(geo, 3);

                System.out.println(geoBuffer);

                //重新覆蓋the_geom屬性的值,這里的geoBuffer必須為Geometry類型

                featureBuf.setAttribute("the_geom", geoBuffer);

            } 

            //將所有數(shù)據(jù)寫入

            writer.write();

            //關(guān)閉寫入流

            writer.close();


本文內(nèi)容根據(jù)網(wǎng)絡(luò)資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點和立場。

轉(zhuǎn)載請注明,如有侵權(quán),聯(lián)系刪除。

本文鏈接:http://gantiao.com.cn/post/18981859.html

發(fā)布評論

您暫未設(shè)置收款碼

請在主題配置——文章設(shè)置里上傳

掃描二維碼手機訪問

文章目錄