Shapefile属于一种矢量图形格式,它能够保存几何图形的位置及相关属性。但这种格式没法存储地理数据的拓扑信息。

其中,要组成一个Shapefile,有三个文件是必不可少的,它们分别是".shp", ".shx"与 ".dbf"文件

  • .shp— 图形格式,用于保存元素的几何实体。

  • .shx— 图形索引格式。几何体位置索引,记录每一个几何体在shp文件之中的位置,能够加快向前或向后搜索一个几何体的效率。

  • .dbf— 属性数据格式,以dBase IV的数据表格式存储每个几何形状的属性数据。

下面将介绍如何通过Java读取Shape文件中的内容信息

我们的pom文件

<?xml version="1.0" encoding="UTF-8"?>
<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.herbert.geotool</groupId>
<artifactId>geo</artifactId>
<version>1.0-SNAPSHOT</version> <dependencies>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-shapefile</artifactId>
<version>19.2</version>
<scope>system</scope>
</dependency> <dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-opengis</artifactId>
<version>19.2</version>
</dependency> <dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-data</artifactId>
<version>19.2</version>
</dependency> <dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-api</artifactId>
<version>19.2</version>
</dependency> <dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-main</artifactId>
<version>19.2</version>
</dependency> <dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-metadata</artifactId>
<version>19.2</version>
</dependency> <dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-referencing</artifactId>
<version>19.2</version>
</dependency> <dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-geojson</artifactId>
<version>19.2</version>
</dependency> <dependency>
<groupId>org.json.simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1</version>
</dependency> <dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool</artifactId>
<version>1.5.4</version>
</dependency> <dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency> <dependency>
<groupId>com.vividsolutions</groupId>
<artifactId>jts</artifactId>
<version>1.13</version>
</dependency>
</dependencies> </project>

具体Java代码

package com.herbert.geotoool.util;

import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.geojson.feature.FeatureJSON;
import org.opengis.feature.simple.SimpleFeature; import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.nio.charset.Charset; /**
* @author :Herbert
* @date :Created in 2019/12/26 17:01
* @description:
* @modified By:
* @version: $
*/ public class ShapeModel { public static void main(String[] args) throws IOException {
long start = System.currentTimeMillis(); String SHAPE_FILE = "F:\\MapData\\gisMap\\xian\\街道界线.shp"; // ShapeFile全路径 // 使用GeoTools读取ShapeFile文件
File shapeFile = new File(SHAPE_FILE);
ShapefileDataStore store = new ShapefileDataStore(shapeFile.toURI().toURL());
//设置编码
Charset charset = Charset.forName("GBK");
store.setCharset(charset);
SimpleFeatureSource sfSource = store.getFeatureSource();
SimpleFeatureIterator sfIter = sfSource.getFeatures().features();
// 从ShapeFile文件中遍历每一个Feature,然后将Feature转为GeoJSON字符串
while (sfIter.hasNext()) {
SimpleFeature feature = (SimpleFeature) sfIter.next();
// Feature转GeoJSON
FeatureJSON fjson = new FeatureJSON();
StringWriter writer = new StringWriter();
fjson.writeFeature(feature, writer);
String sjson = writer.toString();
System.out.println("sjson===== >>>> " + sjson);
}
System.out.println("数据导入完成,共耗时"+(System.currentTimeMillis() - start)+"ms");
}
}

读取数据显示:

JAVA用geotools读取shape格式文件的更多相关文章

  1. JAVA用geotools读写shape格式文件

    转自:http://toplchx.iteye.com/blog/1335007 JAVA用geotools读写shape格式文件 (对应geotools版本:2.7.2) (后面添加对应geotoo ...

  2. Java使用Geotools读取shape矢量数据

    作为GIS开发者而言,矢量数据是我们经常要用到的,而shape数据是矢量数据中最常用的格式,因此解析shape数据也是作为GIS软件开发人员必备的基础技能,而GeoTools无疑是Java最好用来处理 ...

  3. 使用jsp读取TXT格式文件

    <%@page import="java.io.BufferedReader"%> <%@page import="java.io.FileReader ...

  4. 读取siftgeo格式文件的matlab程序

    % This function reads a siftgeo binary file %读取siftgeo格式的二进制文件 % % Usage: [v, meta] = siftgeo_read ( ...

  5. java通过CLASSPATH读取包内文件

    读取包内文件,使用的路径一定是相对的classpath路径,比如a,位于包内,此时可以创建读取a的字节流:InputStream in = ReadFile.class.getResourceAsSt ...

  6. SHELL读取 ini 格式文件做配置文件

    ini文件格式一般都是由节.键.值三部分组成 格式: [第一节 ] 第一个键 = 值 第二个键 = 第二个值 [第二节 ] 第一个键 = val1,val2,val3 例子: [COM] KINGGO ...

  7. Java读取UTF-8格式文件第一行出现乱码——问号“?”及解决 And Java读带有BOM的UTF-8文件乱码原因及解决方法

    測试样例: Java读取UTF-8的txt文件第一行出现乱码"?"及解决 test.txt文件内容: 1 00:00:06,000 --> 00:00:06,010 < ...

  8. [转]World Wind Java开发之五——读取本地shp文件

    World Wind Java 使用IconLayer图层类表现点和多点数据,使用RenderableLayer图层表现线和面数据,一个图层只能对应一组shape文件.World Wind Java首 ...

  9. World Wind Java开发之五——读取本地shp文件(转)

    http://blog.csdn.net/giser_whu/article/details/41484433 World Wind Java 使用IconLayer图层类表现点和多点数据,使用Ren ...

随机推荐

  1. nginx代理php项目的websocket

    在http段添加 http { ... map $http_upgrade $connection_upgrade { default upgrade; '' close; } ... } 在serv ...

  2. Mac 安装Android Studio 及环境变量配置

    我翻开历史一查,这历史没有年代.歪歪斜斜的每页上都写着"仁义道德"几个字,我横竖睡不着,仔细看了半夜,才从字缝里看出来,满本上都写着两个字"吃人"! –鲁迅&l ...

  3. gerrit Q&A

    One or more refs/for/ names blocks change upload 原因 这是错误的原因是底层的git仓库有一些不正确的引用,通常是有些开发者使用过程中,直接推送到git ...

  4. Oracle Compute云快速搭建MySQL Keepalived高可用架构

    最近有个客户在测试Oracle Compute云,他们的应用需要使用MySQL数据库,由于是企业级应用一定要考虑高可用架构,因此有需求要在Oracle Compute云上搭建MySQL高可用集群.客户 ...

  5. hexo+github搭建自己的博客

    之前很早就想用hexo弄一个自己独立的博客了,在博客园也写了很多的博客,不过不喜欢博客园的风格.不过今天,终于折腾成功了,用hexo搭建了一个在github写的博客,开心,后面会将自己以前的博客慢慢迁 ...

  6. 笔记: SpringBoot + VUE实现数据字典展示功能

    最近一直在写前端,写得我贼难受,从能看懂一些基础的代码到整个前端框架撸下来鬼知道我经历了啥(:´д`)ゞ 项目中所用到的下拉菜单的值全部都是有数据库中的数据字典表来提供的,显示给用户的是的清晰的意思, ...

  7. C++走向远洋——38(用对象数组操作长方柱类)

    */ * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:changfangzhu.cpp * 作者:常轩 * 微信公众号 ...

  8. 什么是Activiti

    什么是Activiti Activiti属于工作流引擎的一个开源实现.Activiti由Tom Bayen发起.在2010年5月发布了第一个版本.命名也很有意思的采取了Activities(活动)的化 ...

  9. 前端实现html转pdf方法总结

    最近要搞前端html转pdf的功能.折腾了两天,略有所收,踩了一些坑,所以做些记录,为后来的兄弟做些提示,也算是回馈社区.经过一番调(sou)研(suo)发现html导出pdf一般有这几种方式,各有各 ...

  10. 【Java】机考常用知识

    基本操作 数组 声明数组 方法一: int a[] = null; //声明一维数组 //int[] a = null; 也行,个人习惯 a = new int[10];//分配内存给一维数组 方法二 ...