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

先看下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使用geotools读取shape格式文件的方法的文章就介绍到这了,更多相关java geotools读取shape格式内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • java实现画图板功能

    本文实例为大家分享了java实现画图板的具体代码,供大家参考,具体内容如下 画图板需要满足的功能有: 画线,画三角形,画矩形,画多边形,画曲线 设置不同颜色的画笔 改变窗口大小或最小化窗口后重新打开时图案不消失 画图方法的编写: 创建一个类,继承JPanel类,重写JPanel的绘制方法,重写方法时写上原有的绘制方法,再添加绘制已有的图形的方法.(为了在改变窗口大小和最小化窗口后再打开时图案不消失) 在画图方法中首先创建一个窗体,设置好各项参数后,将窗体设置边框布局.由于继承了JPanel类,重

  • java 2d画图示例分享(用java画图)

    Java 2D API通过扩展抽象窗口工具箱(AWT),为Java程序提供了二维图像,文本和图形的功能.这个复杂的渲染包支持线形图像,文本和图形,为富用户界面,复杂绘图程序和图像处理器开发者提供灵活的,功能强大的框架.Java 2D对象出现在一个平面中,称为用户坐标系空间,和设备坐标系空间.当对象在屏幕或打印机中渲染时,用户空间坐标系被转换成设备空间坐标系. 复制代码 代码如下: import java.awt.BasicStroke;import java.awt.Color;import j

  • Java实现的简单画图板示例

    本文实例讲述了Java实现的简单画图板.分享给大家供大家参考,具体如下: 这个画图板是我好久之前做的,之后浙大的同学需要做课设然后就花了一点时间将它改了一下,变得简单些能够方便扩充功能,同时学习java基础 先截图一下吧,就可以知道有哪些功能了~ 三个分区,上面选择图形,下面选择颜色,立体圆就是一个分形,也先放着不需要的同学可以注释了它 代码很简单,就是JPanel进行分区,得到画笔,同时使用画图的函数就可以做到了 贴代码应该很快就会了~ 主类 package awtDemo; import j

  • java实现画图板上画一条直线

    目标:在画图板上画一条直线,供大家参考,具体内容如下 一.首先需要建立一个界面 过程:1.创建界面对象(Java语言中已经有,可直接创建对象来使用). 2.对于一个界面,我们需要设置它的大小.居中.界面关闭.界面可见. 3.写主方法去实现界面. (目前界面还未实现画图板的功能) public class DrawUI { //显示界面的方法 public void show(){ //创建界面对象 JFrame jframe = new JFrame(); //设置界面的尺寸.位置居中.关闭.标

  • java使用GeoTools读取shp文件并画图的操作代码

    GeoTools是ArcGis地图与java对象的桥梁,恰如jdbc之于oracle与java. shp文件本身是存有地理对象边界坐标.对象中心城市及城市编号的多多边形字符串. 需要使用的依赖如下 <!-- 添加GeoTools依赖 --> <dependency> <groupId>org.geotools</groupId> <artifactId>gt-shapefile</artifactId> <version>

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

    先看下JAVA用geotools读取shape格式文件 Shapefile属于一种矢量图形格式,它能够保存几何图形的位置及相关属性.但这种格式没法存储地理数据的拓扑信息. 其中,要组成一个Shapefile,有三个文件是必不可少的,它们分别是".shp", ".shx"与 ".dbf"文件 .shp- 图形格式,用于保存元素的几何实体. .shx- 图形索引格式.几何体位置索引,记录每一个几何体在shp文件之中的位置,能够加快向前或向后搜索一个几

  • php简单读取.vcf格式文件的方法示例

    本文实例讲述了php简单读取.vcf格式文件的方法.分享给大家供大家参考,具体如下: /** * 读取.vcf格式文件 * @param $filename */ function readCvf($filename){ $file = fopen($filename,"r"); while(! feof($file)) { $line=fgets($file); $encoding = mb_detect_encoding($line, array('GB2312','GBK','U

  • PHP读取XML格式文件的方法总结

    本文实例总结了PHP读取XML格式文件的方法.分享给大家供大家参考,具体如下: books.xml文件: <books> <book> <author>Jack Herrington</author> <title>PHP Hacks</title> <publisher>O'Reilly</publisher> </book> <book> <author>Jack Her

  • Python使用xlrd读取Excel格式文件的方法

    本文实例讲述了Python使用xlrd读取Excel格式文件的方法.分享给大家供大家参考.具体如下: 使用xlrd能够很方便的读取excel文件内容,而且这是个跨平台的库,能够在windows,linux/unix,等平台上面使用,代码如下: import xlrd fname = "sample.xls" bk = xlrd.open_workbook(fname) shxrange = range(bk.nsheets) try: sh = bk.sheet_by_name(&qu

  • C#读取csv格式文件的方法

    本文实例讲述了C#读取csv格式文件的方法.分享给大家供大家参考.具体实现方法如下: 一.CSV文件规则   1 开头是不留空,以行为单位. 2 可含或不含列名,含列名则居文件第一行. 3 一行数据不跨行,无空行. 4 以半角逗号(即,)作分隔符,列为空也要表达其存在. 5 列内容如存在半角逗号(即,)则用半角引号(即',')将该字段值包含起来. 6 列内容如存在半角引号(即")则应替换成半角双引号("")转义,并用半角引号(即"")将该字段值包含起来.

  • pandas 读取各种格式文件的方法

    pandas 读取各种格式文件: 前置工序: import pandas as pd csv 文件读取中文错误处理: utf-8 codec can't decode .... pd.read_csv('c:/mydata/jit.csv',encoding='gb18030') sql 读取: import pymysql conn=pymysql.connect(host='127.0.0.1', user='root', passwd='root', db=' employee') sql

  • Java 使用geotools读取tiff数据的示例代码

    Java中如果要解析tiff,其实很多时候,我们都选择gdal的Java库来实现,毫无疑问,gdal确实在GIS数据处理方面非常的强悍,其实Geotools中很多有关栅格数据的解析,也是基于Gdal进行的封装,今天就简单了解使用GeoTools来解析Tiff数据. package com.dudu.gis; import org.geotools.gce.geotiff.GeoTiffReader; import org.geotools.geometry.GeneralEnvelope; im

  • java生成xml格式文件的方法

    本文实例讲述了java生成xml格式文件的方法.分享给大家供大家参考,具体如下: 这里演示利用Java生成xml格式文件 Demo中所用到的jar包Jdom.jar . 为了方便理解,我写了个Demo import java.io.FileOutputStream; import java.io.IOException; import org.jdom.Document; import org.jdom.Element; import org.jdom.JDOMException; import

  • java项目中读取jdbc.properties文件操作

    java内容 Properties props = Resources.getResourceAsProperties("jdbc.properties"); String url = props.getProperty("jdbc.url"); String driver = props.getProperty("jdbc.driverClass"); String username = props.getProperty("jdbc

随机推荐