学习GeoTools的Data模块的相关内容(下)– (学习GIS【2】)

 

 

地理信息系统中最重要的东西莫过于就是数据了,而且我接触GeoTools的本意就是利用其处理众多标准的Gis格式文件的能力。所以首先从GeoToolsData模块着手来开始学习GeoTools

 

 

 

Data模块的定义见  http://docs.codehaus.org/display/GEOTDOC/10+Data

 

The Data module is all about hoisting data (usually in the form of features) off of external services, disk files etc… into your application. This is where you can finally start putting the toolkit to work.

 

 最后一句说明了这个模块的重要性😛

 

 

 

因为Data模块的类很多,但是无外乎就是处理各种GIS文件格式(Shape,mif/mid…)。下面学习Data模块主要以处理Shapefile格式相关的部分。其他格式文件类似。

 

上面这张图来自 http://docs.codehaus.org/display/GEOTDOC/10+Data

是官方网站上学习和阐述Data模块的资料列表

 

下面就这张图的顺序记录下学习Data模块的心得。

 

 

主要是下面几个类概括了整个Data模块的作用,需要特别注意:

DataStore

DataAccess

FeatureSource

FeatureStore

 

10 Memory DataStore

http://docs.codehaus.org/display/GEOTDOC/10+Memory+DataStore

 

Memory DataStore用来在内存中模拟一个数据源。而不是从某个输入端加载进来的(网络,Shapefile,mid/mid…

 

例如可以创建一个数据源,加入部分需要保存的空间数据,然后整体保存到Shapefile或者mif或者其他格式。。😛

 

如果在MemoryDataStrore中添加修改删除空间数据,添加修改删除空间数据的属性,见

http://docs.codehaus.org/display/GEOTDOC/10+Memory+DataStore

 

 

 

11 Exporting to A Shapefilehttp://docs.codehaus.org/display/GEOTDOC/11+Exporting+to+A+Shapefile

 

 

下面是一个完整的课运行的例子!在跑这个例子的时候出现了小插曲,就是报了一个不是初学者能解决的错误,莫名其妙的:)错误是Fail to connect to ESPG connection,我的版本是GeoTools2.5.5在网上查了下原因,也解决这个问题,在这里顺便把解决这个问题的解决方法给贴出来,在测试这些例子的时候发现GeoToolsAPI使用起来不是太舒服,好多类名字在不同包里有相同的名字,可能是我理解的不是太深,但是对于初学者,这个学习的阻抗还是大了点:)

package com.geoguide.snail.geotools;

 

import java.io.*;

import java.net.*;

import java.util.*;

 

import org.geotools.data.*;

import org.geotools.data.shapefile.indexed.*;

import org.geotools.feature.*;

import org.geotools.referencing.*;

import org.opengis.feature.simple.SimpleFeatureType;

import org.opengis.feature.type.FeatureType;

import org.opengis.referencing.*;

import org.opengis.referencing.crs.*;

public class NewShapefile {

 

  public static void main(String[] args) {

    FileDataStoreFactorySpi factory = new IndexedShapefileDataStoreFactory();

 

    File file = new File(“C://testType.shp”);

    Map map = null;

    try {

      map = Collections.singletonMap(“url”, file.toURI().toURL());

    }

    catch (MalformedURLException ex2) {

      ex2.printStackTrace();

    }

 

    DataStore myData = null;

    try {

      myData = factory.createNewDataStore(map);

      FeatureType featureType = null;

      try {

        featureType = DataUtilities.createType(“my”, “geom:Point,name:String,age:Integer,des:String”);

      }

      catch (SchemaException ex) {

        ex.printStackTrace();

      }

      CoordinateReferenceSystem crs = null;

      try {

        crs = CRS.decode(“EPSG:4326”);

      }

      catch (NoSuchAuthorityCodeException ex3) {

        ex3.printStackTrace();

      }

      catch (FactoryException ex3) {

        ex3.printStackTrace();

      }

      if(myData instanceof IndexedShapefileDataStore){

        ((IndexedShapefileDataStore)myData).forceSchemaCRS(crs);

      }

      myData.createSchema( (SimpleFeatureType) featureType);

    }

    catch (IOException ex1) {

      ex1.printStackTrace();

    }

  }

}

 

 

Shapefile Plugin

http://docs.codehaus.org/display/GEOTDOC/Shapefile+Plugin

GeoTools shapefile文件格式的支持是以插件的形式提供的,GeoTools的框架结构还是很有趣的,通过像TCP协议一样的分层结构来做到分离,然后在每层上提供plugin的嵌入,非常简洁优美的结构。见下图,图片来自http://docs.codehaus.org/display/GEOT/8.1+Architecture

 

 

其他对ArcSDE,WFS的支持都是类似的,在官网中没有放一个完整的操作shapefile的代码,在这里放上一个完整的demo,可以直接运行!

/*

 *    GeoTools – The Open Source Java GIS Tookit

 *    http://geotools.org

 *

 *    (C) 2006-2008, Open Source Geospatial Foundation (OSGeo)

 *

 *    This file is hereby placed into the Public Domain. This means anyone is

 *    free to do whatever they wish with this file. Use it well and enjoy!

 */

package com.geoguide.snail.geotools.demo;

 

import java.io.*;

import java.util.*;

 

import org.geotools.data.*;

import org.geotools.demo.swing.*;

import org.geotools.factory.*;

import org.geotools.feature.*;

import org.geotools.feature.collection.*;

import org.geotools.gui.swing.*;

import org.opengis.feature.Feature;

import org.opengis.feature.simple.SimpleFeature;

import org.opengis.feature.simple.SimpleFeatureType;

import com.vividsolutions.jts.geom.*;

 

/**

 * How to Read a Shapefile.

 * <p>

 * Please visit the GeoTools User Guide: <a

 * href=”http://docs.codehaus.org/display/GEOTDOC/04+How+to+Read+a+Shapefile”>

 *

 * @author Jody Garnett (Refractions Research)

 *

 */

public class ShapefileRead {

 

  public static void main(String[] args) throws Exception {

    System.out.println(“Welcome to GeoTools:” + GeoTools.getVersion());

 

    File file;

    if (args.length == 0) {

      file = ShapeFileDialog.showOpenShapefile(null);

    }

    else {

      file = new File(args[0]);

    }

    if (file == null || !file.exists()) {

      System.exit(1);

    }

 

    Map<String, Object> connect = new HashMap<String, Object> ();

    connect.put(“url”, file.toURI().toURL());

 

    DataStore dataStore = DataStoreFinder.getDataStore(connect);

    String[] typeNames = dataStore.getTypeNames();

    String typeName = typeNames[0];

 

    System.out.println(“Reading content ” + typeName);

 

    FeatureSource<SimpleFeatureType, SimpleFeature> featureSource = dataStore.getFeatureSource(typeName);

    FeatureCollection<SimpleFeatureType, SimpleFeature> collection = featureSource.getFeatures();

 

    class DistanceVisitor

        extends AbstractFeatureVisitor {

      int length = 0;

      double area = 0;

      public void visit(Feature feature) {

        Geometry geometry = (Geometry) feature.getDefaultGeometryProperty().getValue();

        length += geometry.getLength();

        area += geometry.getArea();

      }

    };

    DistanceVisitor distance = new DistanceVisitor();

 

    collection.accepts(distance, new ProgressWindow(null));

    System.out.println(“Total length ” + distance.length);

    System.out.println(“Total area ” + distance.area);

 

  }

}

 

 

 

转载自:https://blog.csdn.net/snailjava/article/details/4267066

You may also like...

退出移动版