首页 > 其他分享 >Geotools处理shape文件

Geotools处理shape文件

时间:2023-04-26 21:44:44浏览次数:32  
标签:文件 Geotools features shape new false typeBuilder featureBuilder

shape文件结构

  • filename.shp: shapes
  • filename.shx: 索引文件
  • filename.dbf: 结构化数据文件
  • filename.qix: 空间索引文件
  • filename.fix: fid索引文件
  • filename.sld: 样式文件

依赖

<dependency>
    <groupId>org.geotools</groupId>
    <artifactId>gt-main</artifactId>
    <version>27.2</version>
</dependency>
<dependency>
    <groupId>org.geotools</groupId>
    <artifactId>gt-shapefile</artifactId>
    <version>27.2</version>
</dependency>

创建连接

连接参数
Parameter required Description
url true .shp文件的url
namespace false FeatureType的URI
create spatial index false 是否创建空间索引,默认true
charset false 解码DBF文件的编码,默认ISO_8859_1
timezone false 解析DBF文件时间的时区
memory mapped buffer false 内存映射,默认false
cache and reuse memory maps false 使用内存映射时,缓存并重用,默认true
enable spatial index false 是否使用空间索引,默认true

代码示例

/**
 * 
 * 创建shape文件
 * 
 * */

FileDataStoreFactorySpi factory = new ShapefileDataStoreFactory();

//新建文件
File file = new File("my.shp");
//datastore
Map<String, Object> params = new HashMap<>();
params.put("url", file.toURI().toURL());
params.put("create spatial index", Boolean.TRUE);
DataStore dataStore = factory.createNewDataStore(params);


//Feature数据定义
SimpleFeatureTypeBuilder typeBuilder = new SimpleFeatureTypeBuilder();
//图层名
typeBuilder.setName("myLayer");
//空间坐标
typeBuilder.setCRS(CRS.decode("EPSG:4490"));
typeBuilder.add("the_geom", Point.class);
//普通属性字段
typeBuilder.add("id", String.class);
typeBuilder.length(10).add("name", String.class);//字段长度
typeBuilder.add("number",Integer.class);
typeBuilder.add("double",Double.class);
typeBuilder.add("time",Date.class);
SimpleFeatureType featureType = typeBuilder.buildFeatureType();
//创建feature定义到shape
dataStore.createSchema(featureType);

/**
 * 写数据到shape文件
 * 
 * */


//shape中feature定义
Map<String, Object> params = new HashMap<>();
params.put("url", file.toURI().toURL());
DataStore dataStore2 = DataStoreFinder.getDataStore(params);
String typeName = dataStore2.getTypeNames()[0];
SimpleFeatureSource featureSource = dataStore2.getFeatureSource(typeName);
SimpleFeatureType schema = featureSource.getSchema();

//事务处理
Transaction transaction = new DefaultTransaction("create");

if (featureSource instanceof SimpleFeatureStore) {
    SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;

    //创建一条数据 feature
    List<SimpleFeature> features = new ArrayList<>();
    GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory();
    SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(schema);
    Point point = geometryFactory.createPoint(new Coordinate(106.69563874,29.563694210810283));
    featureBuilder.set("the_geom",point);
    featureBuilder.set("id","id1");
    featureBuilder.set("name","name1");
    featureBuilder.set("number1",100);
    featureBuilder.set("number2",66.0);
    featureBuilder.set("time",new Date());
    SimpleFeature feature = featureBuilder.buildFeature(null);
    features.add(feature);

    SimpleFeatureCollection collection = new ListFeatureCollection(schema, features);
    featureStore.setTransaction(transaction);
    try {
        //写入shape中
        featureStore.addFeatures(collection);
        transaction.commit();
    } catch (Exception e) {
        e.printStackTrace();
        transaction.rollback();
    } finally {
        transaction.close();
    }
} else {
    System.out.println("写入失败");
}


/**
 * 读取shape文件数据
 * 
 * */
DataStore dataStore3 = new ShapefileDataStore(file.toURI().toURL());

String typeName = dataStore3.getTypeNames()[0];
FeatureSource<SimpleFeatureType, SimpleFeature> source =dataStore3.getFeatureSource(typeName);
Filter filter = Filter.INCLUDE;

FeatureCollection<SimpleFeatureType, SimpleFeature> collection = source.getFeatures(filter);
try (FeatureIterator<SimpleFeature> features = collection.features()) {
    //features必须关闭,否则会造成内存泄漏
    while (features.hasNext()) {
        SimpleFeature feature = features.next();
        feature.getProperties().stream().peek(e-> System.out.println(e.getName().getLocalPart() + " = " + e.getValue()));
    }
}

标签:文件,Geotools,features,shape,new,false,typeBuilder,featureBuilder
From: https://www.cnblogs.com/walkAlwaysInCode/p/17357427.html

相关文章

  • cmake包含单独.hpp文件
    myproject/├──CMakeLists.txt├──main.cpp└──include└──hello.hpp#OpenCVfind_package(OpenCVREQUIRED)include_directories(${OpenCV_INCLUDE_DIRS})#Eigenfind_package(Eigen3REQUIRED)include_directories(${EIGEN3_INCLUDE_DIR})include_dire......
  • <packaging>war</packaging>在pom.xml文件里添加这个会导致404的出现
    Maven,javaweb我在pom.xml文件里面添加了这行代码<packaging>war</packaging>就会导致jsp界面的运行失败但是还无道理呀,以往都可以运行成功的啊我通过一行一行注释代码创建项目找到的答案,找了我好久。<?xmlversion="1.0"encoding="UTF-8"?><projectxmlns="http://maven.......
  • python的文件路径操作(转)
    1.1绝对路径不同操作系统下绝对路径的表现形式是不一样的,以Windows系统为例,一个文件的路径可能是这样的:D:\files\data\ndvi.tif其中:D:\:表示根文件夹,是文件所在的盘符,即D盘。D:\files\data:表示文件所在的文件夹的路径,即D盘的files文件夹的子文件夹data。ndvi.tif:表示文件名......
  • struts1 上传文件
    java代码:packagecom.struts1.form;importorg.apache.struts.action.ActionForm;importorg.apache.struts.upload.FormFile;publicclassUploadFileFormextendsActionForm{ privatestaticfinallongserialVersionUID=1L; privateFormFileformFile; pu......
  • 重启nginx后提示nginx.pid文件丢失了
    //在执行nginx-sreload命令时遇到了错误,因为Nginx尝试在日志目录中读取pid文件,但是找不到它。//这通常是因为Nginx没有在启动时正确地生成该文件引起的。//为了解决这个问题,您可以尝试启动Nginx并手动创建pid文件。请使用以下命令启动Nginx:sudo/www/server/nginx/sbin/......
  • 什么是文件传输,介绍文件传输的发展进程
    什么是文件传输,介绍文件传输的发展进程首先,我们先来认识一下文件传输的定义,了解文件传输的概念,才能够真正了解文件传输软件的发展历程。文件传输(filetransfer),是指将一个文件或其中的一部分从一个计算机系统传到另一个计算机系统。它可能把文件传输至另一计算机中去存储,或访问......
  • iOS描述文件(.mobileprovision)一键申请
    转载:IOS描述文件制作教程iOS描述文件(.mobileprovision)一键申请在主界面上点击描述文件按钮。​编辑切换为居中添加图片注释,不超过140字(可选)  新建ios描述文件然后点击新建,然后输入描述文件名称,描述文件名称字符和数字,自己好辨识就可以。......
  • 关于idea使用Tomcat打开jsp文件页面失败的问题的解决
    问题描述在idea里面使用Tomcat打开jsp文件频繁报错,检查相关路径也没有任何问题,而且用来测试的jsp文件还是最简单的形式,困扰了大概5分钟左右问题解决经过查询百度可知,这次不是路径的问题,而是需要将我们在web文件夹里面创建的jsp文件复制到target文件夹里面保持与target文件夹里......
  • java程序执行exe脚本文件
    一、新建bat脚本文件,并写入执行exe脚本命令: 二、执行bat脚本:1StringfullPath="E:\\model-script\\ComSim-master.bat";2Filefile=newFile(fullPath);3if(file.exists()){//如果已存在,删除旧文件4file.delete();5......
  • 关于电脑脑共享文件无论怎么设置登录名都无法登录的问题。
    先写结论:电脑账户有可能是使用的微软账户登陆的,需要使用本地账户,设置方法是设置-》账户-》改用本地账户登录(好像叫这个),这个过程就是重新设置账户名与密码就可以了。经过:今天想要共享笔记本内的某个文件,但是发现除非设置共享不使用密码,否则无法查看。我的电脑重装过好几次,现在笔......