GeoTools Eclipse 快速入门04

2024-02-24 18:32

本文主要是介绍GeoTools Eclipse 快速入门04,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

我们继续翻译GeoTools官网教程,这节是关于稍微复杂一些的图形操作。

Things to Try


Each tutorial consists of very detailed steps followed by a series of extra questions. If you get stuck at any point please ask your instructor; or sign up to the geotools-users email list.

Here are some additional challenges for you to try:

  • Try out the different sample data sets

  • You can zoom in, zoom out and show the full extents and Use the select tool to examine individual countries in the sample countries.shp file

  • Download the largest shapefile you can find and see how quickly it can be rendered. You should find that the very first time it will take a while as a spatial index is generated. After that performance should be very good when zoomed in.

  • Performance: We know that one of the ways people select a spatial library is based on speed. By design GeoTools does not load the above shapefile into memory (instead it streams it off of disk each time it is drawn using a spatial index to only bring the content required for display).

if you would like to ask GeoTools to cache the shapefile in memory try the following code:

    /**
     * This method demonstrates using a memory-based cache to speed up the display (e.g. when
     * zooming in and out).
     * 
     * There is just one line extra compared to the main method, where we create an instance of
     * CachingFeatureStore.
     */public static void main(String[] args) throws Exception {// display a data store file chooser dialog for shapefilesFile file = JFileDataStoreChooser.showOpenFile("shp", null);if (file == null) {return;}FileDataStore store = FileDataStoreFinder.getDataStore(file);SimpleFeatureSource featureSource = store.getFeatureSource();// CachingFeatureSource is deprecated as experimental (not yet production ready)CachingFeatureSource cache = new CachingFeatureSource(featureSource);// Create a map content and add our shapefile to itMapContent map = new MapContent();map.setTitle("Using cached features");Style style = SLD.createSimpleStyle(featureSource.getSchema());Layer layer = new FeatureLayer(cache, style);map.addLayer(layer);// Now display the mapJMapFrame.showMap(map);}
For the above example to compile hit Control-Shift-O to organise imports; it will pull in the following import:

import org.geotools.data.CachingFeatureSource;

Note

When building you may see a message that CachingFeatureSource is deprecated. It’s ok to ignore it, it’s just a warning. The class is still under test but usable.

  • Try and sort out what all the different “side car” files are - and what they are for. The sample data set includes “shp”, “dbf” and “shx”. How many other side car files are there?
  • Advanced: The use of FileDataStoreFinder allows us to work easily with files. The other way to do things is with a map of connection parameters. This techniques gives us a little more control over how we work with a shapefile and also allows us to connect to databases and web feature servers.
        File file = JFileDataStoreChooser.showOpenFile("shp", null);Map<String,Object> params = new HashMap<>();params.put( "url", file.toURI().toURL() );params.put( "create spatial index", false );params.put( "memory mapped buffer", false );params.put( "charset", "ISO-8859-1" );DataStore store = DataStoreFinder.getDataStore( params );SimpleFeatureSource featureSource = store.getFeatureSource( store.getTypeNames()[0] );

  • Important: GeoTools is an active open source project - you can quickly use maven to try out the latest nightly build by changing your pom.xml file to use a “SNAPSHOT” release.
At the time of writing 17-SNAPSHOT is under active devilopment.

    <properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><!-- use the latest snapshot --><geotools.version>17-SNAPSHOT</geotools.version></properties>
You will also need to change your pom.xml file to include the following snapshot repository:

    <repositories><repository><id>maven2-repository.dev.java.net</id><name>Java.net repository</name><url>http://download.java.net/maven/2</url></repository><repository><id>osgeo</id><name>Open Source Geospatial Foundation Repository</name><url>http://download.osgeo.org/webdav/geotools/</url></repository><repository><snapshots><enabled>true</enabled></snapshots><id>boundless</id><name>Boundless Maven Repository</name><url>http://repo.boundlessgeo.com/main</url></repository></repositories><build><plugins><plugin><inherited>true</inherited><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><source>1.8</source><target>1.8</target></configuration></plugin></plugins></build>
  • So what jars did maven actually use for the Quickstart application? Open up your pom.xml and switch to the dependency heirarchy or dependency graph tabs to see what is going on.

we will be making use of some of the project in greater depth in the remaining tutorials.


试着做点东西


每个教程都包含了非常详细的步骤,其后是一系列额外的问题。如果您在任何地方遇到瓶颈,请询问您的指导员或者到 geotools 的用户列表去注册。

下面有一些额外的挑战供您尝试:

  • 尝试不同的样例数据集

  • 您可以放大、缩小并对完整范围进行全图显示,还可以使用选择工具来检查示例文件countries.shp中的各个国家/地区

  • 下载您能找到的最大的shape文件来测试一下它的渲染速度。您会发现,最开始的时候它需要一段时间来创建空间索引,等之后再进行放大就好了。

  • 性能:我们知道,人们选择一种空间库要考虑的因素之一就是它的速度。GeoTools在设计的时候并没有让上面的shape图形加载到内存中(它采取的方式是:利用一个空间索引,在每次绘制图形的时候只把需要的部分从硬盘中调取出来。)

如果您想让GeoTools 将 shapefile 文件放到内存中去缓存,可以尝试如下代码:
    /**
     * This method demonstrates using a memory-based cache to speed up the display (e.g. when
     * zooming in and out).
     * 
     * There is just one line extra compared to the main method, where we create an instance of
     * CachingFeatureStore.
     */public static void main(String[] args) throws Exception {// display a data store file chooser dialog for shapefilesFile file = JFileDataStoreChooser.showOpenFile("shp", null);if (file == null) {return;}FileDataStore store = FileDataStoreFinder.getDataStore(file);SimpleFeatureSource featureSource = store.getFeatureSource();// CachingFeatureSource is deprecated as experimental (not yet production ready)CachingFeatureSource cache = new CachingFeatureSource(featureSource);// Create a map content and add our shapefile to itMapContent map = new MapContent();map.setTitle("Using cached features");Style style = SLD.createSimpleStyle(featureSource.getSchema());Layer layer = new FeatureLayer(cache, style);map.addLayer(layer);// Now display the mapJMapFrame.showMap(map);}

对于上面的例子,编译命令 Control-Shift-O 来组织导入的内容;如下提到的内容就会被导入:

import org.geotools.data.CachingFeatureSource;

Note

在创建过程中,您可能会看到一个 CachingFeatureSource is deprecated (不推荐使用缓存数据源) 的警告,可以忽略它,这只是个警告而已,这个类仍在测试中,但却可以正常使用。

  • 尝试弄清楚所有的“side car”文件应该归为哪类,它们都是用来做什么的。示例数据集中包含“shp”,"dbf","shx"等文件,想想还有没其他类型的“side car”文件呢?

  • 高级:使用 文件数据存储查找器 FileDataStoreFinder 能让我们的文件操作更加简单。另一种方式是使用一个连接参数的映射。通过这种技术,我们可以更好的操控 shapefile ,也可以去连接数据库或 Web要素服务器。

        File file = JFileDataStoreChooser.showOpenFile("shp", null);Map<String,Object> params = new HashMap<>();params.put( "url", file.toURI().toURL() );params.put( "create spatial index", false );params.put( "memory mapped buffer", false );params.put( "charset", "ISO-8859-1" );DataStore store = DataStoreFinder.getDataStore( params );SimpleFeatureSource featureSource = store.getFeatureSource( store.getTypeNames()[0] );
  • 重要提示:GeoTools是一个活跃的开源项目 - 您可以使用 maven 更改 pom.xml 文件来尝试使用最新的 maven“SNAPSHOT”发行库。
截止本网页编辑的时刻,快照版本 “17-SNASHOT” 正在积极构建中。

    <properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><!-- use the latest snapshot --><geotools.version>17-SNAPSHOT</geotools.version></properties>
您还需要修改您的 pom.xml 文件以使其包含 快照库 snapshot repository 的内容。

    <repositories><repository><id>maven2-repository.dev.java.net</id><name>Java.net repository</name><url>http://download.java.net/maven/2</url></repository><repository><id>osgeo</id><name>Open Source Geospatial Foundation Repository</name><url>http://download.osgeo.org/webdav/geotools/</url></repository><repository><snapshots><enabled>true</enabled></snapshots><id>boundless</id><name>Boundless Maven Repository</name><url>http://repo.boundlessgeo.com/main</url></repository></repositories><build><plugins><plugin><inherited>true</inherited><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><source>1.8</source><target>1.8</target></configuration></plugin></plugins></build>
  • 本快速入门教程的应用程序中都使用到了哪些jar包呢?打开您的 pom.xml 文件并切换到 依赖关系树 dependency heirarchy 或 依赖关系图 deendency graph tabs 来看看是怎么回事吧。

在剩下的教程中,我们将更加深入的利用一些项目来进行学习。

这篇关于GeoTools Eclipse 快速入门04的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/742980

相关文章

使用Python实现快速搭建本地HTTP服务器

《使用Python实现快速搭建本地HTTP服务器》:本文主要介绍如何使用Python快速搭建本地HTTP服务器,轻松实现一键HTTP文件共享,同时结合二维码技术,让访问更简单,感兴趣的小伙伴可以了... 目录1. 概述2. 快速搭建 HTTP 文件共享服务2.1 核心思路2.2 代码实现2.3 代码解读3.

Spring Boot + MyBatis Plus 高效开发实战从入门到进阶优化(推荐)

《SpringBoot+MyBatisPlus高效开发实战从入门到进阶优化(推荐)》本文将详细介绍SpringBoot+MyBatisPlus的完整开发流程,并深入剖析分页查询、批量操作、动... 目录Spring Boot + MyBATis Plus 高效开发实战:从入门到进阶优化1. MyBatis

springboot security快速使用示例详解

《springbootsecurity快速使用示例详解》:本文主要介绍springbootsecurity快速使用示例,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝... 目录创www.chinasem.cn建spring boot项目生成脚手架配置依赖接口示例代码项目结构启用s

解决Java中基于GeoTools的Shapefile读取乱码的问题

《解决Java中基于GeoTools的Shapefile读取乱码的问题》本文主要讨论了在使用Java编程语言进行地理信息数据解析时遇到的Shapefile属性信息乱码问题,以及根据不同的编码设置进行属... 目录前言1、Shapefile属性字段编码的情况:一、Shp文件常见的字符集编码1、System编码

C++快速排序超详细讲解

《C++快速排序超详细讲解》快速排序是一种高效的排序算法,通过分治法将数组划分为两部分,递归排序,直到整个数组有序,通过代码解析和示例,详细解释了快速排序的工作原理和实现过程,需要的朋友可以参考下... 目录一、快速排序原理二、快速排序标准代码三、代码解析四、使用while循环的快速排序1.代码代码1.由快

Win32下C++实现快速获取硬盘分区信息

《Win32下C++实现快速获取硬盘分区信息》这篇文章主要为大家详细介绍了Win32下C++如何实现快速获取硬盘分区信息,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 实现代码CDiskDriveUtils.h#pragma once #include <wtypesbase

Python FastAPI入门安装使用

《PythonFastAPI入门安装使用》FastAPI是一个现代、快速的PythonWeb框架,用于构建API,它基于Python3.6+的类型提示特性,使得代码更加简洁且易于绶护,这篇文章主要介... 目录第一节:FastAPI入门一、FastAPI框架介绍什么是ASGI服务(WSGI)二、FastAP

Spring AI与DeepSeek实战一之快速打造智能对话应用

《SpringAI与DeepSeek实战一之快速打造智能对话应用》本文详细介绍了如何通过SpringAI框架集成DeepSeek大模型,实现普通对话和流式对话功能,步骤包括申请API-KEY、项目搭... 目录一、概述二、申请DeepSeek的API-KEY三、项目搭建3.1. 开发环境要求3.2. mav

Python如何快速下载依赖

《Python如何快速下载依赖》本文介绍了四种在Python中快速下载依赖的方法,包括使用国内镜像源、开启pip并发下载功能、使用pipreqs批量下载项目依赖以及使用conda管理依赖,通过这些方法... 目录python快速下载依赖1. 使用国内镜像源临时使用镜像源永久配置镜像源2. 使用 pip 的并

SpringBoot快速接入OpenAI大模型的方法(JDK8)

《SpringBoot快速接入OpenAI大模型的方法(JDK8)》本文介绍了如何使用AI4J快速接入OpenAI大模型,并展示了如何实现流式与非流式的输出,以及对函数调用的使用,AI4J支持JDK8... 目录使用AI4J快速接入OpenAI大模型介绍AI4J-github快速使用创建SpringBoot