geotools17.1读取shp文件实例 for java


geotools17.1读取shp文件实例 for java
参考url:https://www.cnblogs.com/cugwx/p/3719195.html

引用库

geotools-17.1-bin.zip
JDK1.8

geotoolstest.java文件内容:

package com.cwgis;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.nio.charset.Charset;
import java.util.Collection;
import java.util.Iterator;

import org.geotools.data.FeatureSource;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.shapefile.dbf.DbaseFileHeader;
import org.geotools.data.shapefile.dbf.DbaseFileReader;
import org.geotools.data.shapefile.files.ShpFiles;
import org.geotools.feature.FeatureCollection;
import org.geotools.feature.FeatureIterator;
import org.opengis.feature.Property;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.geometry.primitive.Point;


public class geotoolstest {
    public static void main(String[] args) throws Exception
    {
        String t_path="D:\\数据\\shp_point\\xzq_point.dbf";
        readDBF(t_path);
        String shp_path="D:\\数据\\shp_point\\xzq_point.shp";
        readSHP(shp_path);
    }
    //OK read dbf 
    public static void readDBF(String path) {
        DbaseFileReader reader = null;  
        try {  
            reader = new DbaseFileReader(new ShpFiles(path), false, Charset.forName("GBK"));  
            DbaseFileHeader header = reader.getHeader();  
            int numFields = header.getNumFields();  
            //迭代读取记录  
            while (reader.hasNext()) {  
                try {  
                    Object[] entry = reader.readEntry();  
                    for (int i=0; i<numFields; i++) {  
                        String title = header.getFieldName(i);  
                        Object value = entry[i];  
                        System.out.println(title+"="+value);  
                    }  
                } catch (Exception e) {  
                    e.printStackTrace();  
                }  
            }  
        } catch (Exception e) {  
            e.printStackTrace();  
        } finally {  
            if (reader != null) {  
                //关闭  
                try {reader.close();} catch (Exception e) {}  
            }  
        }  

    }
    //OK read point shp
    public static void readSHP(String path) {
        ShapefileDataStore shpDataStore = null;  
        try{  
            shpDataStore = new ShapefileDataStore(new File(path).toURI().toURL());  
            shpDataStore.setCharset(Charset.forName("GBK"));  
            String typeName = shpDataStore.getTypeNames()[0];  
            FeatureSource<SimpleFeatureType, SimpleFeature> featureSource = null;   
            featureSource = (FeatureSource<SimpleFeatureType, SimpleFeature>)shpDataStore.getFeatureSource(typeName);  
            FeatureCollection<SimpleFeatureType, SimpleFeature> result = featureSource.getFeatures();  
            System.out.println(result.size());  
            FeatureIterator<SimpleFeature> itertor = result.features();  
            while(itertor.hasNext()){  
                SimpleFeature feature = itertor.next();  
                Collection<Property> p = feature.getProperties();  
                Iterator<Property> it = p.iterator();  
                while(it.hasNext()) {  
                    Property pro = it.next();  
                    if (pro.getValue() instanceof Point) { 
                        Point t_p=(Point)(pro.getValue());
                        //System.out.println(t_p.toString());
                        //System.out.println("PointX = " + t_p.getX());  
                        //System.out.println("PointY = " + t_p.getY());  
                    }
                    else if(pro.getName().toString()=="the_geom")
                    {
                        System.out.println(pro.getName() + " => " + pro.getValue()); 
                    }
                    else {  
                        //System.out.println(pro.getName() + " => " + pro.getValue());  
                    }  
                }  
            }  
            itertor.close();  
        } catch (MalformedURLException e) {  
            e.printStackTrace();  
        } catch(IOException e) { e.printStackTrace(); }  
    }
}

执行结果为:
读取属性值:

OBJECTID_1=1
OBJECTID=1
FID_=0
Entity=LWPolyline
Layer=琛屾斂鐣岀嚎
Color=4
Linetype=Continuous
Elevation=0.0
LineWt=25
RefName=
Shape_Leng=13894.9914713
ORIG_FID=0

OBJECTID_1=2
OBJECTID=2
FID_=0
Entity=LWPolyline
Layer=琛屾斂鐣岀嚎
Color=4
Linetype=Continuous
Elevation=0.0
LineWt=25
RefName=
Shape_Leng=17063.7076737
ORIG_FID=1

读取的点坐标:

285
the_geom => POINT (36388555.0035 3054821.5874000005)
the_geom => POINT (36359441.1712 3055231.216)
the_geom => POINT (36390687.9104 3052571.215)
the_geom => POINT (36386699.8618 3053722.6427999996)
the_geom => POINT (36407311.2108 3051726.7277000006)
the_geom => POINT (36438936.8015 3051652.613399999)
the_geom => POINT (36352598.2487 3052500.7599)
the_geom => POINT (36355569.071 3055454.17)

–the—end—

转载自:https://blog.csdn.net/hsg77/article/details/82632047

You may also like...