geoTools使用实例1

geoTools是开源的JAVA GIS工具包,利用它提供的接口,我们可以编写自己的一个地理信息显示查询软件。

记录一下使用geotools的helloWorld程序。

首先,下载geotools的jar包,https://sourceforge.net/projects/geotools/files/,里面有各种版本的geotools jar包,我选择的是16版本的。

然后,在eclipse里新建一个java项目,然后将下载的jar包全部加入到lib中。

再新建一个class,写入下面的代码:


import java.io.File;
import java.nio.charset.Charset;

import org.geotools.data.FileDataStore;
import org.geotools.data.FileDataStoreFinder;
import org.geotools.data.shapefile.ShapefileDataStore;
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;


public class Main {
	public static void main(String[] args) throws Exception{
//		GeotoolsTest geotoolsTest = new GeotoolsTest();
//		geotoolsTest.showMap();
		File file = JFileDataStoreChooser.showOpenFile("shp", null);
        if (file == null) {
            return;
        }

        FileDataStore store = FileDataStoreFinder.getDataStore(file);
        //中文转码,避免乱码
        ((ShapefileDataStore) store).setCharset(Charset.forName("GBK"));
        
        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);
	}
}

点击运行后,会弹出文件选择对话框,这时选择在网上下载的shp文件:

点击打开后,会弹出地图显示框:

显示框的上面是缩放、目标选择、漫游等的功能键。显示框下方的x= ,y= ,是当前鼠标位置的坐标,最右侧是参考系。

点击目标选取工具(上排倒数第二个按钮)后,再次点击一个目标,即可显示目标信息对话框:

helloWorld就完成了。

转载自:https://blog.csdn.net/gaixm/article/details/69372582

You may also like...

退出移动版