JAVA使用geotools读取shape格式文件的方法

所属分类: 软件编程 / java 阅读数: 36
收藏 0 赞 0 分享

先看下JAVA用geotools读取shape格式文件

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 根据数据库表内容生产树结构JSON数据的实例代码

1、利用场景

  组织机构树,通常会有组织机构表,其中有code(代码),pcode(上级代码),name(组织名称)等字段

2、构造数据(以下数据并不是组织机构数据,而纯属本人胡编乱造的数据)

List<Tree<Test>> trees = new ArrayList<Tree<Test>>();
tests.add(new Test("0", "", "关于本人"));
tests.add(new Test("1", "0", "技术学习"));
tests.add(new Test("2", "0", "兴趣"));
tests.add(new Test("3", "1", "JAVA"));
tests.add(new Test("4", "1", "oracle"));
tests.add(new Test("5", "1", "spring"));
tests.add(new Test("6", "1", "springmvc"));
tests.add(new Test("7", "1", "fastdfs"));
tests.add(new Test("8", "1", "linux"));
tests.add(new Test("9", "2", "骑行"));
tests.add(new Test("10", "2", "吃喝玩乐"));
tests.add(new Test("11", "2", "学习"));
tests.add(new Test("12", "3", "String"));
tests.add(new Test("13", "4", "sql"));
tests.add(new Test("14", "5", "ioc"));
tests.add(new Test("15", "5", "aop"));
tests.add(new Test("16", "1", "等等"));
tests.add(new Test("17", "2", "等等"));
tests.add(new Test("18", "3", "等等"));
tests.add(new Test("19", "4", "等等"));
tests.add(new Test("20", "5", "等等"));

3、源码

Tree.java

package pers.kangxu.datautils.bean.tree;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.alibaba.fastjson.JSON;
/**
 * tree TODO <br>
 * 
 * @author kangxu2 2017-1-7
 * 
 */
public class Tree<T> {
 /**
 * 节点ID
 */
 private String id;
 /**
 * 显示节点文本
 */
 private String text;
 /**
 * 节点状态,open closed
 */
 private String state = "open";
 /**
 * 节点是否被选中 true false
 */
 private boolean checked = false;
 /**
 * 节点属性
 */
 private List<Map<String, Object>> attributes;
 /**
 * 节点的子节点
 */
 private List<Tree<T>> children = new ArrayList<Tree<T>>();
 /**
 * 父ID
 */
 private String parentId;
 /**
 * 是否有父节点
 */
 private boolean isParent = false;
 /**
 * 是否有子节点
 */
 private boolean isChildren = false;
 public String getId() {
 return id;
 }
 public void setId(String id) {
 this.id = id;
 }
 public String getText() {
 return text;
 }
 public void setText(String text) {
 this.text = text;
 }
 public String getState() {
 return state;
 }
 public void setState(String state) {
 this.state = state;
 }
 public boolean isChecked() {
 return checked;
 }
 public void setChecked(boolean checked) {
 this.checked = checked;
 }
 public List<Map<String, Object>> getAttributes() {
 return attributes;
 }
 public void setAttributes(List<Map<String, Object>> attributes) {
 this.attributes = attributes;
 }
 public List<Tree<T>> getChildren() {
 return children;
 }
 public void setChildren(List<Tree<T>> children) {
 this.children = children;
 }
 public boolean isParent() {
 return isParent;
 }
 public void setParent(boolean isParent) {
 this.isParent = isParent;
 }
 public boolean isChildren() {
 return isChildren;
 }
 public void setChildren(boolean isChildren) {
 this.isChildren = isChildren;
 }
 public String getParentId() {
 return parentId;
 }
 public void setParentId(String parentId) {
 this.parentId = parentId;
 }
 public Tree(String id, String text, String state, boolean checked,
 List<Map<String, Object>> attributes, List<Tree<T>> children,
 boolean isParent, boolean isChildren, String parentID) {
 super();
 this.id = id;
 this.text = text;
 this.state = state;
 this.checked = checked;
 this.attributes = attributes;
 this.children = children;
 this.isParent = isParent;
 this.isChildren = isChildren;
 this.parentId = parentID;
 }
 public Tree() {
 super();
 }
 @Override
 public String toString() {
 return JSON.toJSONString(this);
 }
}

BuildTree.java

package pers.kangxu.datautils.common.tree;
import java.util.ArrayList;
import java.util.List;
import pers.kangxu.datautils.bean.tree.Tree;
/**
 * 构建tree
 * TODO
 * <br>
 * @author kangxu2 2017-1-7
 *
 */
public class BuildTree {
 /**
 * 
 * TODO
 * <br>
 * @author kangxu2 2017-1-7
 *
 * @param nodes
 * @return
 */
 public static <T> Tree<T> build(List<Tree<T>> nodes) {
 if(nodes == null){
 return null;
 }
 List<Tree<T>> topNodes = new ArrayList<Tree<T>>();
 for (Tree<T> children : nodes) {
 String pid = children.getParentId();
 if (pid == null || "".equals(pid)) {
 topNodes.add(children);
 continue;
 }
 for (Tree<T> parent : nodes) {
 String id = parent.getId();
 if (id != null && id.equals(pid)) {
  parent.getChildren().add(children);
  children.setParent(true);
  parent.setChildren(true);
  continue;
 }
 }
 }
 Tree<T> root = new Tree<T>();
 if (topNodes.size() == 0) {
 root = topNodes.get(0);
 } else {
 root.setId("-1");
 root.setParentId("");
 root.setParent(false);
 root.setChildren(true);
 root.setChecked(true);
 root.setChildren(topNodes);
 root.setText("顶级节点");
 }
 return root;
 }
}

BuildTreeTester.java

package pers.kangxu.datautils.test;
import java.util.ArrayList;
import java.util.List;
import pers.kangxu.datautils.bean.tree.Tree;
import pers.kangxu.datautils.common.tree.BuildTree;
public class BuildTreeTester {
 public static void main(String[] args) {
 List<Tree<Test>> trees = new ArrayList<Tree<Test>>();
 List<Test> tests = new ArrayList<Test>();
 tests.add(new Test("0", "", "关于本人"));
 tests.add(new Test("1", "0", "技术学习"));
 tests.add(new Test("2", "0", "兴趣"));
 tests.add(new Test("3", "1", "JAVA"));
 tests.add(new Test("4", "1", "oracle"));
 tests.add(new Test("5", "1", "spring"));
 tests.add(new Test("6", "1", "springmvc"));
 tests.add(new Test("7", "1", "fastdfs"));
 tests.add(new Test("8", "1", "linux"));
 tests.add(new Test("9", "2", "骑行"));
 tests.add(new Test("10", "2", "吃喝玩乐"));
 tests.add(new Test("11", "2", "学习"));
 tests.add(new Test("12", "3", "String"));
 tests.add(new Test("13", "4", "sql"));
 tests.add(new Test("14", "5", "ioc"));
 tests.add(new Test("15", "5", "aop"));
 tests.add(new Test("16", "1", "等等"));
 tests.add(new Test("17", "2", "等等"));
 tests.add(new Test("18", "3", "等等"));
 tests.add(new Test("19", "4", "等等"));
 tests.add(new Test("20", "5", "等等"));
 for (Test test : tests) {
 Tree<Test> tree = new Tree<Test>();
 tree.setId(test.getId());
 tree.setParentId(test.getPid());
 tree.setText(test.getText());
 trees.add(tree);
 }
 Tree<Test> t = BuildTree.build(trees);
 System.out.println(t);
 }
}
class Test {
 private String id;
 private String pid;
 private String text;
 public String getId() {
 return id;
 }
 public void setId(String id) {
 this.id = id;
 }
 public String getPid() {
 return pid;
 }
 public void setPid(String pid) {
 this.pid = pid;
 }
 public String getText() {
 return text;
 }
 public void setText(String text) {
 this.text = text;
 }
 public Test(String id, String pid, String text) {
 super();
 this.id = id;
 this.pid = pid;
 this.text = text;
 }
 public Test() {
 super();
 }
 @Override
 public String toString() {
 return "Test [id=" + id + ", pid=" + pid + ", text=" + text + "]";
 }
}

4、运行结果

JSON数据:

{
 "checked": true,
 "children": [
 {
 "checked": false,
 "children": [
 {
  "checked": false,
  "children": [
  {
  "checked": false,
  "children": [
  {
   "checked": false,
   "children": [],
   "id": "12",
   "parent": true,
   "parentId": "3",
   "state": "open",
   "text": "String"
  },
  {
   "checked": false,
   "children": [],
   "id": "18",
   "parent": true,
   "parentId": "3",
   "state": "open",
   "text": "等等"
  }
  ],
  "id": "3",
  "parent": true,
  "parentId": "1",
  "state": "open",
  "text": "JAVA"
  },
  {
  "checked": false,
  "children": [
  {
   "checked": false,
   "children": [],
   "id": "13",
   "parent": true,
   "parentId": "4",
   "state": "open",
   "text": "sql"
  },
  {
   "checked": false,
   "children": [],
   "id": "19",
   "parent": true,
   "parentId": "4",
   "state": "open",
   "text": "等等"
  }
  ],
  "id": "4",
  "parent": true,
  "parentId": "1",
  "state": "open",
  "text": "oracle"
  },
  {
  "checked": false,
  "children": [
  {
   "checked": false,
   "children": [],
   "id": "14",
   "parent": true,
   "parentId": "5",
   "state": "open",
   "text": "ioc"
  },
  {
   "checked": false,
   "children": [],
   "id": "15",
   "parent": true,
   "parentId": "5",
   "state": "open",
   "text": "aop"
  },
  {
   "checked": false,
   "children": [],
   "id": "20",
   "parent": true,
   "parentId": "5",
   "state": "open",
   "text": "等等"
  }
  ],
  "id": "5",
  "parent": true,
  "parentId": "1",
  "state": "open",
  "text": "spring"
  },
  {
  "checked": false,
  "children": [],
  "id": "6",
  "parent": true,
  "parentId": "1",
  "state": "open",
  "text": "springmvc"
  },
  {
  "checked": false,
  "children": [],
  "id": "7",
  "parent": true,
  "parentId": "1",
  "state": "open",
  "text": "fastdfs"
  },
  {
  "checked": false,
  "children": [],
  "id": "8",
  "parent": true,
  "parentId": "1",
  "state": "open",
  "text": "linux"
  },
  {
  "checked": false,
  "children": [],
  "id": "16",
  "parent": true,
  "parentId": "1",
  "state": "open",
  "text": "等等"
  }
  ],
  "id": "1",
  "parent": true,
  "parentId": "0",
  "state": "open",
  "text": "技术学习"
 },
 {
  "checked": false,
  "children": [
  {
  "checked": false,
  "children": [],
  "id": "9",
  "parent": true,
  "parentId": "2",
  "state": "open",
  "text": "骑行"
  },
  {
  "checked": false,
  "children": [],
  "id": "10",
  "parent": true,
  "parentId": "2",
  "state": "open",
  "text": "吃喝玩乐"
  },
  {
  "checked": false,
  "children": [],
  "id": "11",
  "parent": true,
  "parentId": "2",
  "state": "open",
  "text": "学习"
  },
  {
  "checked": false,
  "children": [],
  "id": "17",
  "parent": true,
  "parentId": "2",
  "state": "open",
  "text": "等等"
  }
  ],
  "id": "2",
  "parent": true,
  "parentId": "0",
  "state": "open",
  "text": "兴趣"
 }
 ],
 "id": "0",
 "parent": false,
 "parentId": "",
 "state": "open",
 "text": "关于本人"
 }
 ],
 "id": "-1",
 "parent": false,
 "parentId": "",
 "state": "open",
 "text": "顶级节点"
}
更多精彩内容其他人还在看

Java的面向对象编程基本概念学习笔记整理

这篇文章主要介绍了Java的面向对象编程基本概念学习笔记整理,包括类与方法以及多态等支持面向对象语言中的重要特点,需要的朋友可以参考下
收藏 0 赞 0 分享

Eclipse下编写java程序突然不会自动生成R.java文件和包的解决办法

这篇文章主要介绍了Eclipse下编写java程序突然不会自动生成R.java文件和包的解决办法 的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

基于Java实现杨辉三角 LeetCode Pascal's Triangle

这篇文章主要介绍了基于Java实现杨辉三角 LeetCode Pascal's Triangle的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Java中Spring获取bean方法小结

Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架,如何在程序中获取Spring配置的bean呢?下面通过本文给大家介绍Java中Spring获取bean方法小结,对spring获取bean方法相关知识感兴趣的朋友一起学习吧
收藏 0 赞 0 分享

如何计算Java对象占用了多少空间?

在Java中没有sizeof运算符,所以没办法知道一个对象到底占用了多大的空间,但是在分配对象的时候会有一些基本的规则,我们根据这些规则大致能判断出来对象大小,需要的朋友可以参考下
收藏 0 赞 0 分享

剖析Java中的事件处理与异常处理机制

这篇文章主要介绍了Java中的事件处理与异常处理机制,讲解Java是如何对事件或者异常作出响应以及定义异常的一些方法,需要的朋友可以参考下
收藏 0 赞 0 分享

详解Java的Struts2框架的结构及其数据转移方式

这篇文章主要介绍了详解Java的Struts2框架的结构及其数据转移方式,Struts框架是Java的SSH三大web开发框架之一,需要的朋友可以参考下
收藏 0 赞 0 分享

Java封装好的mail包发送电子邮件的类

本文给大家分享了2个java封装好的mail包发送电子邮件的类,并附上使用方法,小伙伴们可以根据自己的需求自由选择。
收藏 0 赞 0 分享

在Java的Struts中判断是否调用AJAX及用拦截器对其优化

这篇文章主要介绍了在Java的Struts中判断是否调用AJAX及用拦截器对其优化的方法,Struts框架是Java的SSH三大web开发框架之一,需要的朋友可以参考下
收藏 0 赞 0 分享

java多线程Future和Callable类示例分享

JAVA多线程实现方式主要有三种:继承Thread类、实现Runnable接口、使用ExecutorService、Callable、Future实现有返回结果的多线程。其中前两种方式线程执行完后都没有返回值,只有最后一种是带返回值的。今天我们就来研究下Future和Callab
收藏 0 赞 0 分享
查看更多