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 shapefiles
        File 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 it
        MapContent 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 map
        JMapFrame.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 shapefiles
        File 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 it
        MapContent 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 map
        JMapFrame.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 来看看是怎么回事吧。
在剩下的教程中,我们将更加深入的利用一些项目来进行学习。


转载自:https://blog.csdn.net/SuperGiser_Lee/article/details/73330806

You may also like...