geotools学习1--org.geotools.demo例子FirstProject
来源:互联网 发布:unity3d玻璃材质下载 编辑:程序博客网 时间:2024/05/24 05:48
geotools学习--org.geotools.demo例子FirstProject
转载自:http://blog.csdn.net/hengcai001/article/details/4342073
代码段:
/* * 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! * *--程序分析说明 *2009年7月12日 * * * * */package org.geotools.demo;/* * 常用java包的引入 */import java.io.File;import java.io.FileNotFoundException;import java.io.Serializable;import java.util.HashMap;import java.util.Map;/* * GUI界面包的引入 */import javax.swing.JFileChooser;import javax.swing.filechooser.FileFilter;/* * geotools 包的引入 */import org.geotools.data.DataStore;import org.geotools.data.DataStoreFinder;import org.geotools.data.FeatureSource;import org.geotools.factory.GeoTools;import org.geotools.feature.FeatureCollection;import org.geotools.feature.FeatureIterator;/*8 * geoApi包的引入 * GeoAPI为OpenGIS规范提供一组Java接口 */import org.opengis.feature.simple.SimpleFeature;import org.opengis.feature.simple.SimpleFeatureType;/* * jts 包的引入 */import com.vividsolutions.jts.geom.Geometry;/** * The example code for the "FirstProject" in the GeoTools wiki. * <p> * This code matches these examples: * <ul> * <li><a href="http://docs.codehaus.org/display/GEOTDOC/03+First+Project" mce_href="http://docs.codehaus.org/display/GEOTDOC/03+First+Project">First Project</a> * <li><a href="http://docs.codehaus.org/display/GEOTDOC/04+How+to+Read+a+Shapefile" mce_href="http://docs.codehaus.org/display/GEOTDOC/04+How+to+Read+a+Shapefile">How to Read a Shapefile</a> * </ul> * * @author Jody Garnett */public class FirstProject {public static void main(String[] args) throws Exception {/* * 输入程序版本 */System.out.println("Welcome to GeoTools:" + GeoTools.getVersion());/* * 打开输入对话框 */File file = promptShapeFile(args);try {// Connection parameters/* * 获取连接参数 * */Map<String, Serializable> connectParameters = new HashMap<String, Serializable>();connectParameters.put("url", file.toURI().toURL());connectParameters.put("create spatial index", true);/* * 创建DataStore * DataStore为接口 */DataStore dataStore = DataStoreFinder.getDataStore(connectParameters);// we are now connectedString[] typeNames = dataStore.getTypeNames();String typeName = typeNames[0];System.out.println("Reading content " + typeName);/* * 图形信息 * 比较复杂(需要仔细研究 ) */FeatureSource<SimpleFeatureType, SimpleFeature> featureSource;FeatureCollection<SimpleFeatureType, SimpleFeature> collection;FeatureIterator<SimpleFeature> iterator;featureSource = dataStore.getFeatureSource(typeName);collection = featureSource.getFeatures();iterator = collection.features();double totalLength = 0.0;try {while (iterator.hasNext()) {SimpleFeature feature = iterator.next();/* * GeoMetry来自JavaTopologicalSuite */Geometry geometry = (Geometry) feature.getDefaultGeometry();totalLength += geometry.getLength();}} finally {if (iterator != null) {// YOU MUST CLOSE THE ITERATOR!iterator.close();}}System.out.println("Total Length " + totalLength);} catch (Exception ex) {ex.printStackTrace();System.exit(1);}System.exit(0);}/** * Prompt for File if not provided on the command line. * Don't forget the quotes around your path if there are spaces! * * @throws FileNotFoundException * 打开shp文件对话框 * 获取相关参数 * 用到了两个重要的系统类 * (1)FIle类 * (2)JFileChooser类 */private static File promptShapeFile(String[] args)throws FileNotFoundException {File file;/* * * 如果没有输入参数执行下面的代码 */if (args.length == 0) {JFileChooser chooser = new JFileChooser();//文件对话框chooser.setDialogTitle("Open Shapefile for Reprojection");/* * FileFilter为抽象类 * 需要实例化两个方法 * (1)accept(); * (2)getDescription(); */chooser.setFileFilter(new FileFilter() {public boolean accept(File f) {return f.isDirectory() || f.getPath().endsWith("shp")|| f.getPath().endsWith("SHP");}public String getDescription() {return "Shapefiles";}});/* *打开对话框 */int returnVal = chooser.showOpenDialog(null);/* * 判断是够选择了yes还是NO */if (returnVal != JFileChooser.APPROVE_OPTION) {System.exit(0);}file = chooser.getSelectedFile();/* * 成功输出正确的文件名 */System.out.println("You chose to open this file: " + file.getName());} else {/* * 直接提供参数说明 * */file = new File(args[0]);}/* * 最后验证file是否存在 * 如果不存在则显示抛出异常 */if (!file.exists()) {throw new FileNotFoundException(file.getAbsolutePath());}return file;}}
- geotools学习1--org.geotools.demo例子FirstProject
- GeoTools学习
- geotools学习
- GeoTools学习篇------GeoTools体系
- GeoTools学习篇------初步了解GeoTools
- GeoTools学习篇------GeoTools+Eclipse搭建QuickStart
- GeoTools学习(referencing)
- GeoTools学习笔记--MapLayer
- GeoTools学习笔记--MapContext
- Geotools使用-1
- geoTools使用实例1
- GEoTools学习笔记---显示三维效果--geotools-renderer3d
- 使用GeoTools读取和绘制Shapefile格式 -- (学习GIS【1】)
- Geotools 简介
- 什么是geotools
- 什么是geotools
- geotools 实例
- 说一说Geotools
- jquery实现新闻漂亮的动画展示效果
- ssh 错误: ssh_exchange_identification
- Mysql5.5.22源码编译安装
- [原创] Linux体系结构(一): Linux的前世今生
- Linux 系统cpu利用率计算
- geotools学习1--org.geotools.demo例子FirstProject
- Ruby rails 页面跳转(render和redirect_to)
- 第15条:使可变性最小化(创建不可改变类)
- linux中的ftok函数
- windows 启动函数的作用
- Java线程(二):线程同步synchronized和volatile
- 字节编码类型判断<转>
- TCP/IP层次
- Ubuntu使用指南