欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

geotools系列1-读取shp文件 博客分类: GIS geotoolsshpgisjava 

程序员文章站 2024-02-24 20:42:28
...

.shp文件是目前大家使用频率较高而且通用的一种格式。此篇主要说明geotools对shp文件的读取说明。

 

1、maven依赖,包含了postgis支持、epsg、和swing的支持,后续的文章基本都基于此

 

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.jjxliu.geotools</groupId>
  <artifactId>geotools_t1</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>geotools_t1</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
     <geotools.version>20-SNAPSHOT</geotools.version>
  </properties>
<dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-shapefile</artifactId>
            <version>${geotools.version}</version>
        </dependency>
        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-swing</artifactId>
            <version>${geotools.version}</version>
        </dependency>
        <dependency>
  			<groupId>org.geotools</groupId>
  			<artifactId>gt-jdbc</artifactId>
  			<version>${geotools.version}</version>
		</dependency>
        <dependency>
   			<groupId>org.geotools.jdbc</groupId>
   			<artifactId>gt-jdbc-postgis</artifactId>
   			<version>${geotools.version}</version>
 		</dependency>
 	
 		<dependency>
   			<groupId>org.geotools</groupId>
   			<artifactId>gt-epsg-hsql</artifactId>
   			<version>${geotools.version}</version>
 		</dependency>
 		
    </dependencies>
     <repositories>
        <repository>
            <id>maven2-repository.dev.java.net</id>
            <name>Java.net repository</name>
            <url>http://download.java.net/maven/2</url>
        </repository>
        <repository>
            <id>osgeo</id>
            <name>Open Source Geospatial Foundation Repository</name>
            <url>http://download.osgeo.org/webdav/geotools/</url>
        </repository>
        <repository>
          <snapshots>
            <enabled>true</enabled>
          </snapshots>
          <id>boundless</id>
          <name>Boundless Maven Repository</name>
          <url>http://repo.boundlessgeo.com/main</url>
        </repository>
    </repositories>
     <build>
        <plugins>
            <plugin>
                <inherited>true</inherited>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

 

 

2、直接上java代码说明shp文件读取

public static SimpleFeatureCollection  readShp(String path ){
		return readShp(path, null);
		
	}

	public static SimpleFeatureCollection  readShp(String path , Filter filter){
	  
		SimpleFeatureSource  featureSource = readStoreByShp(path);
		   
		if(featureSource == null) return null;
          
		try {
			return filter != null ? featureSource.getFeatures(filter) : featureSource.getFeatures() ;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
         
         return null ;
	}
	
	public static  SimpleFeatureSource readStoreByShp(String path ){
		 
		File file = new File(path);
    	 
		FileDataStore store;
		SimpleFeatureSource featureSource = null;
		try {
			store = FileDataStoreFinder.getDataStore(file);
			 ((ShapefileDataStore) store).setCharset(Charset.forName("UTF-8"));
			featureSource = store.getFeatureSource();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
         
         return featureSource ;
	}

 

3、测试一段读取的代码,打印所有读取到的simplefeature

String path1 = "G:/work/china_map/shp/BOUNT_poly.shp" ;
		
		//读取shp
		SimpleFeatureCollection  colls1 = readShp(path1);
		//拿到所有features
		SimpleFeatureIterator iters = colls1.features();
		//遍历打印
		while(iters.hasNext()){
			SimpleFeature sf = iters.next();
			
			System.out.println(sf.getID() + " , " + sf.getAttributes());
			
		}

 

打印贴图


geotools系列1-读取shp文件
            
    
    博客分类: GIS geotoolsshpgisjava 
 

 

实例的.java在附件。

 

 

 

  • geotools系列1-读取shp文件
            
    
    博客分类: GIS geotoolsshpgisjava 
  • 大小: 36 KB