欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2020年03月25日 17:04:48   作者:摘星族  
這篇文章主要介紹了JAVA使用geotools讀取shape格式文件的方法,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

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

Shapefile屬于一種矢量圖形格式,它能夠保存幾何圖形的位置及相關(guān)屬性。但這種格式?jīng)]法存儲地理數(shù)據(jù)的拓?fù)湫畔ⅰ?/p>

其中,要組成一個Shapefile,有三個文件是必不可少的,它們分別是".shp", ".shx"與 ".dbf"文件

  • .shp— 圖形格式,用于保存元素的幾何實體。
  • .shx— 圖形索引格式。幾何體位置索引,記錄每一個幾何體在shp文件之中的位置,能夠加快向前或向后搜索一個幾何體的效率。
  • .dbf— 屬性數(shù)據(jù)格式,以dBase IV的數(shù)據(jù)表格式存儲每個幾何形狀的屬性數(shù)據(jù)。

下面將介紹如何通過Java讀取Shape文件中的內(nèi)容信息

我們的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());
  //設(shè)置編碼
  Charset charset = Charset.forName("GBK");
  store.setCharset(charset);
  SimpleFeatureSource sfSource = store.getFeatureSource();
  SimpleFeatureIterator sfIter = sfSource.getFeatures().features();
  // 從ShapeFile文件中遍歷每一個Feature,然后將Feature轉(zhuǎn)為GeoJSON字符串
  while (sfIter.hasNext()) {
   SimpleFeature feature = (SimpleFeature) sfIter.next();
   // Feature轉(zhuǎn)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("數(shù)據(jù)導(dǎo)入完成,共耗時"+(System.currentTimeMillis() - start)+"ms");
 }
}

讀取數(shù)據(jù)顯示:

補充:JAVA 根據(jù)數(shù)據(jù)庫表內(nèi)容生產(chǎn)樹結(jié)構(gòu)JSON數(shù)據(jù)的實例代碼

1、利用場景

  組織機構(gòu)樹,通常會有組織機構(gòu)表,其中有code(代碼),pcode(上級代碼),name(組織名稱)等字段

2、構(gòu)造數(shù)據(jù)(以下數(shù)據(jù)并不是組織機構(gòu)數(shù)據(jù),而純屬本人胡編亂造的數(shù)據(jù))

List<Tree<Test>> trees = new ArrayList<Tree<Test>>();
tests.add(new Test("0", "", "關(guān)于本人"));
tests.add(new Test("1", "0", "技術(shù)學(xué)習(xí)"));
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", "學(xué)習(xí)"));
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> {
 /**
 * 節(jié)點ID
 */
 private String id;
 /**
 * 顯示節(jié)點文本
 */
 private String text;
 /**
 * 節(jié)點狀態(tài),open closed
 */
 private String state = "open";
 /**
 * 節(jié)點是否被選中 true false
 */
 private boolean checked = false;
 /**
 * 節(jié)點屬性
 */
 private List<Map<String, Object>> attributes;
 /**
 * 節(jié)點的子節(jié)點
 */
 private List<Tree<T>> children = new ArrayList<Tree<T>>();
 /**
 * 父ID
 */
 private String parentId;
 /**
 * 是否有父節(jié)點
 */
 private boolean isParent = false;
 /**
 * 是否有子節(jié)點
 */
 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;
/**
 * 構(gòu)建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("頂級節(jié)點");
 }
 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", "", "關(guān)于本人"));
 tests.add(new Test("1", "0", "技術(shù)學(xué)習(xí)"));
 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", "學(xué)習(xí)"));
 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、運行結(jié)果

JSON數(shù)據(jù):

{
 "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": "技術(shù)學(xué)習(xí)"
 },
 {
  "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": "學(xué)習(xí)"
  },
  {
  "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": "關(guān)于本人"
 }
 ],
 "id": "-1",
 "parent": false,
 "parentId": "",
 "state": "open",
 "text": "頂級節(jié)點"
}

總結(jié)

到此這篇關(guān)于JAVA使用geotools讀取shape格式文件的方法的文章就介紹到這了,更多相關(guān)java geotools讀取shape格式內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Springboot主程序類注解配置過程圖解

    Springboot主程序類注解配置過程圖解

    這篇文章主要介紹了Springboot主程序類注解配置過程圖解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-10-10
  • java實現(xiàn)浮點數(shù)轉(zhuǎn)人民幣的小例子

    java實現(xiàn)浮點數(shù)轉(zhuǎn)人民幣的小例子

    java實現(xiàn)浮點數(shù)轉(zhuǎn)人民幣的小例子,需要的朋友可以參考一下
    2013-03-03
  • SpringBoot2自動裝配原理解析

    SpringBoot2自動裝配原理解析

    這篇文章主要介紹了SpringBoot2自動裝配原理解析,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-03-03
  • 淺談Java基于Consul創(chuàng)建分布式鎖

    淺談Java基于Consul創(chuàng)建分布式鎖

    這篇文章主要介紹了淺談基于Consul創(chuàng)建分布式鎖,Consul是HashiCorp公司推出的開源工具,用于實現(xiàn)分布式系統(tǒng)的服務(wù)發(fā)現(xiàn)與配置Consul是分布式的、高可用的、可橫向擴展的,需要的朋友可以參考下
    2023-07-07
  • 淺談java監(jiān)聽器的作用

    淺談java監(jiān)聽器的作用

    這篇文章主要介紹了淺談java監(jiān)聽器的作用,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-09-09
  • Java實現(xiàn)AES加密算法的簡單示例分享

    Java實現(xiàn)AES加密算法的簡單示例分享

    這篇文章主要介紹了Java實現(xiàn)AES加密算法的簡單示例分享,AES算法是基于對密碼值的置換和替代,需要的朋友可以參考下
    2016-04-04
  • String.trim()消除不了空格的問題及解決

    String.trim()消除不了空格的問題及解決

    這篇文章主要介紹了String.trim()消除不了空格的問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • SpringMVC之@InitBinder注解詳解

    SpringMVC之@InitBinder注解詳解

    這篇文章主要介紹了SpringMVC之@InitBinder注解詳解,springmvc并不是能對所有類型的參數(shù)進行綁定的,如果對日期Date類型參數(shù)進行綁定,就會報錯IllegalStateException錯誤,需要的朋友可以參考下
    2024-01-01
  • java圖論普利姆及克魯斯卡算法解決最小生成樹問題詳解

    java圖論普利姆及克魯斯卡算法解決最小生成樹問題詳解

    這篇文章主要為大家介紹了java圖論普利姆算法及克魯斯卡算法解決最小生成樹問題的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助
    2021-11-11
  • spring schedule配置多任務(wù)動態(tài)cron(增刪啟停)

    spring schedule配置多任務(wù)動態(tài)cron(增刪啟停)

    這篇文章主要介紹了spring schedule配置多任務(wù)動態(tài)cron(增刪啟停),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03

最新評論