This topic is intended to help users quickly build a Ganos development environment for test purposes by coding. After you read this topic, you will learn how to manage Ganos spatio-temporal data, including how to connect to a database, create an index, import data, and query data.

Dependencies:

<dependencies>
        <dependency>
            <groupId>org.locationtech.geomesa</groupId>
            <artifactId>geomesa-hbase-datastore_2.11</artifactId>
            <version>${geomesa.version}</version>
            <exclusions>
                <exclusion>
                    <groupId>org.apache.hbase</groupId>
                    <artifactId>hbase-client</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.apache.hbase</groupId>
                    <artifactId>hbase-server</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.apache.hbase</groupId>
                    <artifactId>hbase-common</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.apache.hbase</groupId>
                    <artifactId>hbase-protocol</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>com.aliyun.hbase</groupId>
            <artifactId>alihbase-client</artifactId>
            <version>${hbase.version}</version>
            <exclusions>
                <exclusion>
                    <artifactId>com.google.guava</artifactId>
                    <groupId>guava</groupId>
                </exclusion>
            </exclusions>
        </dependency>


        <dependency>
            <groupId>com.aliyun.hbase</groupId>
            <artifactId>alihbase-server</artifactId>
            <version>${hbase.version}</version>
        </dependency>
        <dependency>
            <groupId>com.aliyun.hbase</groupId>
            <artifactId>alihbase-common</artifactId>
            <version>${hbase.version}</version>
        </dependency>
        <dependency>
            <groupId>com.aliyun.hbase</groupId>
            <artifactId>alihbase-protocol</artifactId>
            <version>${hbase.version}</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.83</version>
        </dependency>
    </dependencies>
    
    
    <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>
        

Sample code and descriptions

package com.aliyun.tst;

import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.Point;
import org.geotools.data.DataStore;
import org.geotools.data.DataStoreFinder;
import org.geotools.data.Query;
import org.geotools.data.Transaction;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.data.simple.SimpleFeatureWriter;
import org.geotools.factory.CommonFactoryFinder;
import org.geotools.factory.Hints;
import org.geotools.feature.simple.SimpleFeatureBuilder;
import org.geotools.filter.text.ecql.ECQL;
import org.geotools.geometry.jts.JTSFactoryFinder;
import org.locationtech.geomesa.utils.geotools.SimpleFeatureTypes;
import org.opengis.feature.Feature;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.filter.FilterFactory2;
import org.opengis.filter.sort.SortBy;
import org.opengis.filter.sort.SortOrder;

import java.io.BufferedReader;
import java.io.FileReader;
import java.text.SimpleDateFormat;
import java.util.*;

public class Demo {
    public static final String ZK = "localhost"; //The ZooKeeper endpoint.
    public static void main(String args[]){
        try{
            DataStore ds=null;
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd' 'HH:mm:ss");

            //Configure connection parameters.
            Map<String, String> params= new HashMap<>();
            params.put("hbase.zookeepers",ZK);
            params.put("hbase.catalog","test_catalog");

            //Initialize the DataStore.
            ds= DataStoreFinder.getDataStore(params);

            //Use SimpleFeatureType to define the table schema.
            String sft_name="point";
            SimpleFeatureType sft=
                    SimpleFeatureTypes.createType(sft_name, "name:String,dtg:Date,*geom:Point:srid=4326");

            //Specify Gzip as the compression algorithm.
            sft.getUserData().put("geomesa.table.compression.enabled", "true");
            sft.getUserData().put("geomesa.table.compression.type", "gz");
            
            //Create a table.
            ds.createSchema(sft);

            /*
             * Create a GeometryFactory object.
             */
            GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory();
            SimpleFeatureBuilder builder = new SimpleFeatureBuilder(sft);

            //Construct spatial data (points).
            Point point1 = geometryFactory.createPoint(new Coordinate(120.301,35.086));
            Point point2 = geometryFactory.createPoint(new Coordinate(120.301,35.076));
            Point point3 = geometryFactory.createPoint(new Coordinate(120.301,35.066));

            //Construct SimpleFeatures of points.
            List<SimpleFeature> features=new ArrayList<>();
            features.add(builder.buildFeature("1", new Object[]{"point1",new Date(),point1}));
            features.add(builder.buildFeature("2", new Object[]{"point2",new Date(),point2}));
            features.add(builder.buildFeature("3", new Object[]{"point3",new Date(),point3}));

            //Import SimpleFeature content.
            SimpleFeatureWriter writer=(SimpleFeatureWriter)ds.getFeatureWriterAppend(sft_name, Transaction.AUTO_COMMIT);
            for(SimpleFeature feature:features){
                SimpleFeature toWrite=writer.next();
                toWrite.setAttributes(feature.getAttributes());
                toWrite.getUserData().putAll(feature.getUserData());
                writer.write();
            }
            writer.close();


            //Construct spatio-temporal query conditions.
            long t1=format.parse("2019-01-19 11:45:00").getTime();
            long t2=format.parse("2019-02-21 12:15:00").getTime();

            String sortField="dtg";//Specify the sorting field. In this example, the data is sorted by date.
            FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2();
            SortBy[] sort = new SortBy[]{ff.sort(sortField, SortOrder.DESCENDING)};

            //Construct a query object.
            Query query = new Query(sft_name, ECQL.toFilter( "bbox(geom,120,20,130,40) AND dtg >= "+t1+" AND dtg <= "+t2));
            query.setSortBy(sort);
            SimpleFeatureCollection result=ds.getFeatureSource(sft_name).getFeatures(query);
            SimpleFeatureIterator iterator=result.features();

            //Print the query result.
            long sum = 0;
            while (iterator.hasNext()) {
                System.out.println(iterator.next());
                sum++;
            }
            System.out.println("Total number of queries:" + sum);

        }
        catch (Exception e){
            e.printStackTrace();
        }
    }
}

        

After you successfully run the preceding script, the three "Point" SimpleFeatures are printed. The query result is as follows: