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虚拟环境完全指南

《从入门到精通详解Python虚拟环境完全指南》Python虚拟环境是一个独立的Python运行环境,它允许你为不同的项目创建隔离的Python环境,下面小编就来和大家详细介绍一下吧... 目录什么是python虚拟环境一、使用venv创建和管理虚拟环境1.1 创建虚拟环境1.2 激活虚拟环境1.3 验证虚

Python多线程实现大文件快速下载的代码实现

《Python多线程实现大文件快速下载的代码实现》在互联网时代,文件下载是日常操作之一,尤其是大文件,然而,网络条件不稳定或带宽有限时,下载速度会变得很慢,本文将介绍如何使用Python实现多线程下载... 目录引言一、多线程下载原理二、python实现多线程下载代码说明:三、实战案例四、注意事项五、总结引

C#使用Spire.XLS快速生成多表格Excel文件

《C#使用Spire.XLS快速生成多表格Excel文件》在日常开发中,我们经常需要将业务数据导出为结构清晰的Excel文件,本文将手把手教你使用Spire.XLS这个强大的.NET组件,只需几行C#... 目录一、Spire.XLS核心优势清单1.1 性能碾压:从3秒到0.5秒的质变1.2 批量操作的优雅

Java List 使用举例(从入门到精通)

《JavaList使用举例(从入门到精通)》本文系统讲解JavaList,涵盖基础概念、核心特性、常用实现(如ArrayList、LinkedList)及性能对比,介绍创建、操作、遍历方法,结合实... 目录一、List 基础概念1.1 什么是 List?1.2 List 的核心特性1.3 List 家族成

Mybatis-Plus 3.5.12 分页拦截器消失的问题及快速解决方法

《Mybatis-Plus3.5.12分页拦截器消失的问题及快速解决方法》作为Java开发者,我们都爱用Mybatis-Plus简化CRUD操作,尤其是它的分页功能,几行代码就能搞定复杂的分页查询... 目录一、问题场景:分页拦截器突然 “失踪”二、问题根源:依赖拆分惹的祸三、解决办法:添加扩展依赖四、分页

c++日志库log4cplus快速入门小结

《c++日志库log4cplus快速入门小结》文章浏览阅读1.1w次,点赞9次,收藏44次。本文介绍Log4cplus,一种适用于C++的线程安全日志记录API,提供灵活的日志管理和配置控制。文章涵盖... 目录简介日志等级配置文件使用关于初始化使用示例总结参考资料简介log4j 用于Java,log4c

史上最全MybatisPlus从入门到精通

《史上最全MybatisPlus从入门到精通》MyBatis-Plus是MyBatis增强工具,简化开发并提升效率,支持自动映射表名/字段与实体类,提供条件构造器、多种查询方式(等值/范围/模糊/分页... 目录1.简介2.基础篇2.1.通用mapper接口操作2.2.通用service接口操作3.进阶篇3

Python自定义异常的全面指南(入门到实践)

《Python自定义异常的全面指南(入门到实践)》想象你正在开发一个银行系统,用户转账时余额不足,如果直接抛出ValueError,调用方很难区分是金额格式错误还是余额不足,这正是Python自定义异... 目录引言:为什么需要自定义异常一、异常基础:先搞懂python的异常体系1.1 异常是什么?1.2

Python实现Word转PDF全攻略(从入门到实战)

《Python实现Word转PDF全攻略(从入门到实战)》在数字化办公场景中,Word文档的跨平台兼容性始终是个难题,而PDF格式凭借所见即所得的特性,已成为文档分发和归档的标准格式,下面小编就来和大... 目录一、为什么需要python处理Word转PDF?二、主流转换方案对比三、五套实战方案详解方案1:

使用Redis快速实现共享Session登录的详细步骤

《使用Redis快速实现共享Session登录的详细步骤》在Web开发中,Session通常用于存储用户的会话信息,允许用户在多个页面之间保持登录状态,Redis是一个开源的高性能键值数据库,广泛用于... 目录前言实现原理:步骤:使用Redis实现共享Session登录1. 引入Redis依赖2. 配置R