geotools学习1--org.geotools.demo例子FirstProject

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 connected
			String[] 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;
	}
}


 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值