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

相关文章

从入门到精通MySQL联合查询

《从入门到精通MySQL联合查询》:本文主要介绍从入门到精通MySQL联合查询,本文通过实例代码给大家介绍的非常详细,需要的朋友可以参考下... 目录摘要1. 多表联合查询时mysql内部原理2. 内连接3. 外连接4. 自连接5. 子查询6. 合并查询7. 插入查询结果摘要前面我们学习了数据库设计时要满

从入门到精通C++11 <chrono> 库特性

《从入门到精通C++11<chrono>库特性》chrono库是C++11中一个非常强大和实用的库,它为时间处理提供了丰富的功能和类型安全的接口,通过本文的介绍,我们了解了chrono库的基本概念... 目录一、引言1.1 为什么需要<chrono>库1.2<chrono>库的基本概念二、时间段(Durat

解析C++11 static_assert及与Boost库的关联从入门到精通

《解析C++11static_assert及与Boost库的关联从入门到精通》static_assert是C++中强大的编译时验证工具,它能够在编译阶段拦截不符合预期的类型或值,增强代码的健壮性,通... 目录一、背景知识:传统断言方法的局限性1.1 assert宏1.2 #error指令1.3 第三方解决

Linux如何快速检查服务器的硬件配置和性能指标

《Linux如何快速检查服务器的硬件配置和性能指标》在运维和开发工作中,我们经常需要快速检查Linux服务器的硬件配置和性能指标,本文将以CentOS为例,介绍如何通过命令行快速获取这些关键信息,... 目录引言一、查询CPU核心数编程(几C?)1. 使用 nproc(最简单)2. 使用 lscpu(详细信

从入门到精通MySQL 数据库索引(实战案例)

《从入门到精通MySQL数据库索引(实战案例)》索引是数据库的目录,提升查询速度,主要类型包括BTree、Hash、全文、空间索引,需根据场景选择,建议用于高频查询、关联字段、排序等,避免重复率高或... 目录一、索引是什么?能干嘛?核心作用:二、索引的 4 种主要类型(附通俗例子)1. BTree 索引(

Redis 配置文件使用建议redis.conf 从入门到实战

《Redis配置文件使用建议redis.conf从入门到实战》Redis配置方式包括配置文件、命令行参数、运行时CONFIG命令,支持动态修改参数及持久化,常用项涉及端口、绑定、内存策略等,版本8... 目录一、Redis.conf 是什么?二、命令行方式传参(适用于测试)三、运行时动态修改配置(不重启服务

MySQL DQL从入门到精通

《MySQLDQL从入门到精通》通过DQL,我们可以从数据库中检索出所需的数据,进行各种复杂的数据分析和处理,本文将深入探讨MySQLDQL的各个方面,帮助你全面掌握这一重要技能,感兴趣的朋友跟随小... 目录一、DQL 基础:SELECT 语句入门二、数据过滤:WHERE 子句的使用三、结果排序:ORDE

一文详解如何在idea中快速搭建一个Spring Boot项目

《一文详解如何在idea中快速搭建一个SpringBoot项目》IntelliJIDEA作为Java开发者的‌首选IDE‌,深度集成SpringBoot支持,可一键生成项目骨架、智能配置依赖,这篇文... 目录前言1、创建项目名称2、勾选需要的依赖3、在setting中检查maven4、编写数据源5、开启热

Python中OpenCV与Matplotlib的图像操作入门指南

《Python中OpenCV与Matplotlib的图像操作入门指南》:本文主要介绍Python中OpenCV与Matplotlib的图像操作指南,本文通过实例代码给大家介绍的非常详细,对大家的学... 目录一、环境准备二、图像的基本操作1. 图像读取、显示与保存 使用OpenCV操作2. 像素级操作3.

eclipse如何运行springboot项目

《eclipse如何运行springboot项目》:本文主要介绍eclipse如何运行springboot项目问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目js录当在eclipse启动spring boot项目时出现问题解决办法1.通过cmd命令行2.在ecl