geotools quick start to learning IntelliJ

Posted by eevan79 on Sun, 20 Feb 2022 11:17:53 +0100

title: "geotools learning (I) quick start to IntelliJ"
date: 2021-04-29 14:08:52
tags: []
published: true
hideInList: false
feature:
isTop: false

This guide will help you set up the IntelliJ IDE to use GeoTools and follow the rest of the GeoTools tutorial.

geotools learning (I) quick start to IntelliJ

Preparatory knowledge

This guide assumes the following:

1. You have installed the latest JDK(8 at the time of writing). If not, Eclipse Quickstart provides instructions on how to do this.

2. You have installed IntelliJ. The goal of this article is IntelliJ CE 2016; However, versions at least 13 years ago should work. The final version should also work well. IntelliJ can be downloaded from JetBrains and is usually available out of the box on a normal operating system.

Create a new project

First, we will create a new project using the Maven quick start prototype.

1. Select File - > new project from the menu. In the New Project dialog box, select Maven project, ensure that Create from prototype is selected, and select org apache. Maven: Maven archetype QuickStart prototype. Press one.

image

2. In the next step, we will ask for the basic identification information of our project:

    GroupId: org.geotools

    ArtifactId: tutorial

    Version: 1.0-SNAPSHOT

image

3. Click next. The following screen should be able to retain the default settings. For our purposes, the binding Maven of IntelliJ should be no problem unless the version is less than 3. In this case, you should consider using a new external version.

image

4. Click next. Give this project a name (this name is only used inside IntelliJ), and the tutorial can achieve our goal. You can change the project location as needed and want to keep more default settings (recommended)

image

5. Click finish and our new project will be created. IntelliJ will show us our newly created Maven file and make an initial Maven build (it shouldn't take long to complete this build before trying the next step). IntelliJ should also ask if you want to enable automatic import for Maven dependencies. In this tutorial, it will automatically detect our changes to the POM file and import them automatically.

image

IntelliJ creates an empty App with a simple Hello World java ! And JUnit test cases. You can run them by right clicking App or AppTest in Project Explorer and selecting run from the context menu.

Add a jar package to the project

pom. The XML file describes the structure, configuration, dependencies, and many other aspects of the project. We will focus on the dependencies required for the project.

When downloading jar Maven, Maven uses a "local repository" to store copies if the dependencies it downloads exist.

Maven downloads jar, GeoTools and other projects from the public repository on the internet and publishes his work on the internet.

1. Open pom.com under the root directory of the project XML file. You can see some of the information we entered through the wizard.

2. We will add three things to this file. First, at the top of the converted file, we want to add an attribute element to define the version of the geographic tool we want to use. Although you may want to try different versions, this workbook is written for 23-SNAPSHOT.

For products, the stable version of 23 should be used for geotools version:

<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <geotools.version>23-SNAPSHOT</geotools.version>
 </properties>

3. Add a jar package dependency of GT main and GT swing of geotools, and use the geotools defined above Version replaces version number

 <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-shapefile</artifactId>
            <version>${geotools.version}</version>
        </dependency>
        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-swing</artifactId>
            <version>${geotools.version}</version>
        </dependency>
    </dependencies>

4. Finally, we need to list the external repositories from which maven can download GeoTools and other required jar s.

 <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>

5. If you want to use Java 8 language level features (such as lambdas), you need to tell Maven to use 1.8 source code level

<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>

6. For simplicity, you can pom.xml Download File

You may find that cutting and pasting is much easier than writing

be careful

1.If Maven Dependencies were not automatically downloaded for some reason(Automatic import may be turned off),You can right-click the item and select Maven -> Reimport To manually download dependencies.
2.If you want to download for your dependencies Javadoc,You can go to again Maven Context menu and select download Documentation

Quick start application

Now that our environment has been set up, we can create a simple quick start. This example will display a shapefile on the screen.
1. Let's go to org geotools. tutorial. Create a class called Quickstart in the Quickstart package. IntelliJ can create packages and classes for us at one time; Right click on org The geotools package selects the new - > java class in the project panel and context menu.

2. Add the following code

/*
 *    GeoTools - The Open Source Java GIS Toolkit
 *    http://geotools.org
 *
 *    (C) 2019, Open Source Geospatial Foundation (OSGeo)
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License as published by the Free Software Foundation;
 *    version 2.1 of the License.
 *
 *    This library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *    Lesser General Public License for more details.
 *
 */

package org.geotools.tutorial.quickstart;

import java.io.File;
import org.geotools.data.FileDataStore;
import org.geotools.data.FileDataStoreFinder;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.map.FeatureLayer;
import org.geotools.map.Layer;
import org.geotools.map.MapContent;
import org.geotools.styling.SLD;
import org.geotools.styling.Style;
import org.geotools.swing.JMapFrame;
import org.geotools.swing.data.JFileDataStoreChooser;

/**
 * Prompts the user for a shapefile and displays the contents on the screen in a map frame.
 *
 * <p>This is the GeoTools Quickstart application used in documentationa and tutorials. *
 */
public class Quickstart {

    /**
     * GeoTools Quickstart demo application. Prompts the user for a shapefile and displays its
     * contents on the screen in a map frame
     */
    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();

        // Create a map content and add our shapefile to it
        MapContent map = new MapContent();
        map.setTitle("Quickstart");

        Style style = SLD.createSimpleStyle(featureSource.getSchema());
        Layer layer = new FeatureLayer(featureSource, style);
        map.addLayer(layer);

        // Now display the map
        JMapFrame.showMap(map);
    }
}

1. We need to download some sample data to use. http://www.naturalearthdata.com/ The project is a great project supported by the North American Cartographic Information Association. Go to the link below to download some cultural carriers. You can use "Download all 50m cultural themes" at the top.
1:50m cultural carrier
Please unzip the above data to a location you can easily find, such as the desktop.
1:50m Cultural Vectors
Unzip to an easy to find place
2. Run the application to open the file selector. Select a shapefile from the sample dataset

3. The application will connect to your shapefile, generate a map, and display the shapefile

Code example considerations

1.shapefile is not loaded into memory -- it is read from disk every time it is needed, which allows you to process data sets larger than available memory.
2. We use a very basic display style here, which only displays the function overview. In the following example, we will see how to specify more complex styles

Try something

    1.Try different sample data sets
    2.You can zoom in, out, and display the entire range, and use select The tool examines each of the sample countries. shp file
    3.Download the biggest you can find shapefile,See how fast it can be rendered. You should find that,
      It takes some time to generate the spatial index for the first time. After that, the performance should be very good when zooming in.
    4.Generally speaking, I can't shapefile Instead of loading it into memory, it is placed on disk to create the control index and load it on demand

If you want to ask GeoTools to cache shapefile s 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).
     *
     * <p>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();
        SimpleFeatureSource cachedSource =
                DataUtilities.source(
                        new SpatialIndexFeatureCollection(featureSource.getFeatures()));

        // 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(cachedSource, style);
        map.addLayer(layer);

        // Now display the map
        JMapFrame.showMap(map);
    }

This code cannot be compiled at first because we are missing an import. IntelliJ should prompt to import missing classes immediately. Press ALT enter (enter ^ - on OS X) to open a dialog box to import the missing classes.

You can easily process files using FileDataStoreFinder. Another method is to use connection parameter mapping. This technology gives us more control over how to use shapefile s and allows us to connect to databases and web function 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]);

Author: MR swilder
Link: https://www.jianshu.com/p/b18cbee5af21
Source: Jianshu
The copyright belongs to the author. For commercial reprint, please contact the author for authorization. For non-commercial reprint, please indicate the source.

Topics: Scala gis