Hello GEOTools

近来接触到地理开发中间件,开源框架的GEOTools,感觉蛮好玩的,和大家分享下。

一个地道的GISer对这个开源框架的入门是很简单的,因为整个框架的架构其实和ESRI的AE架构差不多​,下面是一个GEOTools版的Hello World,刚刚入门所以源码中有误的地方请多多指教一起学习。

package New;

import java.io.File;

import org.geotools.data.CachingFeatureSource;

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.styling.Style;

import org.geotools.swing.JMapFrame;

import org.geotools.swing.data.JFileDataStoreChooser;

public class New {

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

   {

       // 打开一个shp文件

   File fileDir=new File("F:\\arcengine\\data");

   File file =JFileDataStoreChooser.showOpenFile("shp", fileDir, null);

//        File file = JFileDataStoreChooser.showOpenFile("shp", null);

//        if (file == null) {

//            return;

//        }

   //存储在store中

       FileDataStore store = FileDataStoreFinder.getDataStore(file);

       SimpleFeatureSource featureSource = store.getFeatureSource();

       //存储在内存中在每次移动图层就不需要再重新读取SHP

       // 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(featureSource, style);

//        Layer layer = new FeatureLayer(cache, style);

       map.addLayer(layer);

       // Now display the map

       JMapFrame.showMap(map);

   }

}

在运行过程中​会报一下错误:

有提示过时的引用:

import org.geotools.data.CachingFeatureSource;​

​以上代码参照其官网,整体能够实现SHP的显示​。


Hello GEOTools

​​