geotool 导出shp_Java 读取shape文件
发布日期:2021-06-24 10:02:38 浏览次数:3 分类:技术文章

本文共 3757 字,大约阅读时间需要 12 分钟。

C# 的话建议使用ArcEngine进行开发,由于各版本不兼容,改为采用基于Java 的GeoTool进行读取

pom依赖如下

19.1

osgeo

Open Source Geospatial Foundation Repository

http://download.osgeo.org/webdav/geotools/

geosolutions

geosolutions repository

http://maven.geo-solutions.it/

true

true

org.slf4j

jcl-over-slf4j

1.7.5

org.slf4j

slf4j-api

1.7.5

org.slf4j

slf4j-log4j12

1.7.5

nl.pdok

geoserver-manager

1.7.0-pdok2

junit

junit

4.11

test

org.geotools

gt-shapefile

${geotools.version}

org.geotools

gt-swing

${geotools.version}

org.geotools

gt-jdbc

${geotools.version}

org.geotools.jdbc

gt-jdbc-postgis

${geotools.version}

org.geotools

gt-epsg-hsql

${geotools.version}

package readshape;

import org.geotools.data.FileDataStore;

import org.geotools.data.FileDataStoreFinder;

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.opengis.feature.simple.SimpleFeature;

import org.opengis.filter.Filter;

import com.vividsolutions.jts.geom.Coordinate;

import com.vividsolutions.jts.geom.Geometry;

import com.vividsolutions.jts.geom.MultiPolygon;

import com.vividsolutions.jts.geom.Polygon;

import java.io.File;

import java.io.IOException;

import java.nio.charset.Charset;

public class Read {

public static void main(String[] args){

String path1 = "E:\\Test\\TestOpenLayers\\data\\wafangdian_0\\wafangdianshi_0.shp" ;

//读取shp

SimpleFeatureCollection colls1 = readShp(path1);

//拿到所有features

SimpleFeatureIterator iters = colls1.features();

//遍历打印

while(iters.hasNext()){

SimpleFeature sf = iters.next();

Object ss= sf.getDefaultGeometry();

System.out.println(ss);

System.err.println(sf.getDefaultGeometryProperty().getValue());

Geometry shape = (Geometry)ss;

System.err.println(shape.toText());

//打印出来的都是相同的内容 MULTIPOLYGON (((107.92820000000006 26.66963000000004, 107.92820000000006 26.69963000000007, 107.95820000000003 26.69963000000007, 107.95820000000003 26.66963000000004, 107.92820000000006 26.66963000000004)))

// 可以直接拼接sql写入到postgresql

/*INSERT INTO table( geom )

VALUES

(

st_geomfromtext('MULTIPOLYGON (((107.92820000000006 26.66963000000004, 107.92820000000006 26.69963000000007, 107.95820000000003 26.69963000000007, 107.95820000000003 26.66963000000004, 107.92820000000006 26.66963000000004)))', 4326)

)

*/

// class com.vividsolutions.jts.geom.MultiPolygon

if(ss instanceof Polygon){

Polygon polygon = (Polygon)ss;

Coordinate[] coordinates= polygon.getCoordinates();

}

else if(ss instanceof MultiPolygon){

MultiPolygon multiPolygon = (MultiPolygon)ss;

String mult=multiPolygon.toString();

Coordinate[] coordinates= multiPolygon.getCoordinates();

System.err.println(coordinates.length);

}

System.err.println(ss.getClass().toString());

System.out.println(sf.getID() + " , " + sf.getAttributes());

}

}

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) {

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) {

e.printStackTrace();

}

return featureSource ;

}

}

标签:geotools,读取,ss,data,org,shape,import,Java,featureSource

来源: https://www.cnblogs.com/zt2710/p/12112528.html

转载地址:https://blog.csdn.net/weixin_31990755/article/details/113018532 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:mysql 关联更新_MySQL UPDATE多表关联更新
下一篇:labview自动保存报表_基于LabVIEW的Excel报表的自动生成功能

发表评论

最新留言

路过,博主的博客真漂亮。。
[***.116.15.85]2024年04月09日 08时16分49秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章