• Geotools之“Hello World”——打开本地shp文件并显示


    概述:从本节开始,博文中会陆陆续续更新一些有关geotools相关的文章。本节讲述的是geotools的开胃菜,打开本地shp文件,并在窗口中显示。


    引言:geotools简介。

    Geotools是Java语言编写的开源GIS工具包。该项目已有十多年历史,生命力旺盛,代码非常丰富,包含多个开源GIS项目,并且基于标准的GIS接口。Geotools主要提供各种GIS算法,各种数据格式的读写和显示。在显示方面要差一些,只是用Swing实现了地图的简单查看和操作。但是用户可以根据Geotools提供的算法自己实现地图的可视化。OpenJump和udig就是基于Geotools的。

    Geotools用到的两个较重要的开源GIS工具包是JTS和GeoAPI。前者主要是实现各种GIS拓扑算法,也是基于GeoAPI的。但是由于两个工具包的GeoAPI分别采用不同的Java代码实现,所以在使用时需要相互转化。Geotools又根据两者定义了部分自己的GeoAPI,所以代码显得臃肿,有时容易混淆。由于GeoAPI进展缓慢,Geotools自己对其进行了扩充。另外,Geotools现在还只是基于2D图形的,缺乏对3D空间数据算法和显示的支持。

    Geotools是Java语言编写的开源GIS工具包。该项目已有十多年历史,生命力旺盛,代码非常丰富,包含多个开源GIS项目,并且基于标准的GIS接口。Geotools主要提供各种GIS算法,各种数据格式的读写和显示。在显示方面要差一些,只是用Swing实现了地图的简单查看和操作。但是用户可以根据Geotools提供的算法自己实现地图的可视化。OpenJump和udig就是基于Geotools的。

    Geotools用到的两个较重要的开源GIS工具包是JTS和GeoAPI。前者主要是实现各种GIS拓扑算法,也是基于GeoAPI的。但是由于两个工具包的GeoAPI分别采用不同的Java代码实现,所以在使用时需要相互转化。Geotools又根据两者定义了部分自己的GeoAPI,所以代码显得臃肿,有时容易混淆。由于GeoAPI进展缓慢,Geotools自己对其进行了扩充。另外,Geotools现在还只是基于2D图形的,缺乏对3D空间数据算法和显示的支持。

    来源:http://blog.csdn.net/geodesy/article/details/5379613


    首先,贴出来效果.


    文件选择


    打开shp文件


    查看对象属性

    接下来,说说实现方式。

    1、下载geotools

    你可以从geotools官方网站http://geotools.org/下载,但是这个网站有时候会连接不上,为方便下载,我上传至我的百度网盘,下载地址为:http://pan.baidu.com/s/1ATwr8。


    2、新建工程,并导入geotools包

    在eclipse中新建——java工程,即可,并添加geotools的jar引用。


    3、新建class

    新建一个java类,代码如下:

    package com.lzugis.geotools;
    
    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.swing.JMapFrame;
    import org.geotools.swing.data.JFileDataStoreChooser;
    
    public class Quickstart {
    	
        /**
         * This method demonstrates using a memory-based cache to speed up the display (e.g. when
         * zooming in and out).
         * 
         * 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();
            
            // CachingFeatureSource is deprecated as experimental (not yet production ready)
            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(cache, style);
            map.addLayer(layer);
    
            // Now display the map
            JMapFrame.showMap(map);
        }
        
    }
    上述代码即可打开shp并显示。




  • 相关阅读:
    CF 7C. Line(扩展欧几里德)
    HDU 1700 Points on Cycle(向量旋转)
    POJ 1673 EXOCENTER OF A TRIANGLE(垂心)
    SRM 594 DIV1 250
    Codeforces Round #207 (Div. 1) A. Knight Tournament(STL)
    POJ 1654 Area(水题)
    POJ 1474 Video Surveillance(半平面交)
    POJ 1473 There's Treasure Everywhere!
    POJ 1329 Circle Through Three Points(三角形外心)
    POJ 1279 Art Gallery(半平面交)
  • 原文地址:https://www.cnblogs.com/lzugis/p/6539841.html
Copyright © 2020-2023  润新知