geotools学习

GeoTools Java 官网下载地址:

http://sourceforge.net/projects/geotools/files/

CSDN资源下载地址:http://download.csdn.net/detail/mike_caoyong/4529917

 

 

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

  1. /* 
  2.  *    GeoTools – The Open Source Java GIS Tookit 
  3.  *    http://geotools.org 
  4.  *  
  5.  *    (C) 2006-2008, Open Source Geospatial Foundation (OSGeo) 
  6.  * 
  7.  *    This file is hereby placed into the Public Domain. This means anyone is 
  8.  *    free to do whatever they wish with this file. Use it well and enjoy! 
  9.  *  
  10.  *–程序分析说明 
  11.  *2009年7月12日  
  12.  *  
  13.  *  
  14.  *  
  15.  *  
  16.  */  
  17. package org.geotools.demo;  
  18. /* 
  19.  * 常用java包的引入 
  20.  */  
  21. import java.io.File;  
  22. import java.io.FileNotFoundException;  
  23. import java.io.Serializable;  
  24. import java.util.HashMap;  
  25. import java.util.Map;  
  26. /* 
  27.  * GUI界面包的引入 
  28.  */  
  29. import javax.swing.JFileChooser;  
  30. import javax.swing.filechooser.FileFilter;  
  31. /* 
  32.  * geotools 包的引入  
  33.  */  
  34. import org.geotools.data.DataStore;  
  35. import org.geotools.data.DataStoreFinder;  
  36. import org.geotools.data.FeatureSource;  
  37. import org.geotools.factory.GeoTools;  
  38. import org.geotools.feature.FeatureCollection;  
  39. import org.geotools.feature.FeatureIterator;  
  40. /*8 
  41.  * geoApi包的引入 
  42.  * GeoAPI为OpenGIS规范提供一组Java接口 
  43.  */  
  44. import org.opengis.feature.simple.SimpleFeature;  
  45. import org.opengis.feature.simple.SimpleFeatureType;  
  46. /* 
  47.  * jts 包的引入 
  48.  */  
  49. import com.vividsolutions.jts.geom.Geometry;  
  50. /** 
  51.  * The example code for the “FirstProject” in the GeoTools wiki. 
  52.  * <p> 
  53.  * This code matches these examples: 
  54.  * <ul> 
  55.  * <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> 
  56.  * <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> 
  57.  * </ul> 
  58.  *  
  59.  * @author Jody Garnett 
  60.  */  
  61. public class FirstProject {  
  62.     public static void main(String[] args) throws Exception {  
  63.         /* 
  64.          * 输入程序版本 
  65.          */  
  66.         System.out.println(“Welcome to GeoTools:” + GeoTools.getVersion());  
  67.         /* 
  68.          * 打开输入对话框 
  69.          */  
  70.         File file = promptShapeFile(args);  
  71.         try {  
  72.             // Connection parameters  
  73.             /* 
  74.              * 获取连接参数 
  75.              *  
  76.              */  
  77.             Map<String, Serializable> connectParameters = new HashMap<String, Serializable>();  
  78.             connectParameters.put(“url”, file.toURI().toURL());  
  79.             connectParameters.put(“create spatial index”true);  
  80.             /* 
  81.              * 创建DataStore  
  82.              * DataStore为接口 
  83.              */  
  84.             DataStore dataStore = DataStoreFinder.getDataStore(connectParameters);  
  85.             // we are now connected  
  86.             String[] typeNames = dataStore.getTypeNames();  
  87.             String typeName = typeNames[0];  
  88.             System.out.println(“Reading content “ + typeName);  
  89.             /* 
  90.              * 图形信息 
  91.              * 比较复杂(需要仔细研究 ) 
  92.              */  
  93.             FeatureSource<SimpleFeatureType, SimpleFeature> featureSource;  
  94.             FeatureCollection<SimpleFeatureType, SimpleFeature> collection;  
  95.             FeatureIterator<SimpleFeature> iterator;  
  96.             featureSource = dataStore.getFeatureSource(typeName);  
  97.             collection = featureSource.getFeatures();  
  98.             iterator = collection.features();  
  99.             double totalLength = 0.0;  
  100.             try {  
  101.                 while (iterator.hasNext()) {  
  102.                     SimpleFeature feature = iterator.next();  
  103.                     /* 
  104.                      * GeoMetry来自JavaTopologicalSuite 
  105.                      */  
  106.                     Geometry geometry = (Geometry) feature.getDefaultGeometry();  
  107.                     totalLength += geometry.getLength();  
  108.                 }  
  109.             } finally {  
  110.                 if (iterator != null) {  
  111.                     // YOU MUST CLOSE THE ITERATOR!  
  112.                     iterator.close();  
  113.                 }  
  114.             }  
  115.             System.out.println(“Total Length “ + totalLength);  
  116.         } catch (Exception ex) {  
  117.             ex.printStackTrace();  
  118.             System.exit(1);  
  119.         }  
  120.         System.exit(0);  
  121.     }  
  122.     /**  
  123.      * Prompt for File if not provided on the command line. 
  124.      * Don’t forget the quotes around your path if there are spaces! 
  125.      *  
  126.      * @throws FileNotFoundException  
  127.      * 打开shp文件对话框 
  128.      * 获取相关参数 
  129.      * 用到了两个重要的系统类 
  130.      * (1)FIle类 
  131.      * (2)JFileChooser类 
  132.      */  
  133.     private static File promptShapeFile(String[] args)  
  134.             throws FileNotFoundException {  
  135.         File file;  
  136.         /* 
  137.          *  
  138.          * 如果没有输入参数执行下面的代码 
  139.          */  
  140.         if (args.length == 0) {  
  141.             JFileChooser chooser = new JFileChooser();//文件对话框  
  142.             chooser.setDialogTitle(“Open Shapefile for Reprojection”);  
  143.             /* 
  144.              * FileFilter为抽象类 
  145.              * 需要实例化两个方法 
  146.              * (1)accept(); 
  147.              * (2)getDescription(); 
  148.              */  
  149.             chooser.setFileFilter(new FileFilter() {  
  150.                 public boolean accept(File f) {  
  151.                     return f.isDirectory() || f.getPath().endsWith(“shp”)  
  152.                             || f.getPath().endsWith(“SHP”);  
  153.                 }  
  154.                 public String getDescription() {  
  155.                     return “Shapefiles”;  
  156.                 }  
  157.             });  
  158.             /* 
  159.              *打开对话框  
  160.              */  
  161.             int returnVal = chooser.showOpenDialog(null);  
  162.               
  163.             /* 
  164.              * 判断是够选择了yes还是NO 
  165.              */  
  166.             if (returnVal != JFileChooser.APPROVE_OPTION) {  
  167.                 System.exit(0);  
  168.             }  
  169.               
  170.             file = chooser.getSelectedFile();  
  171.             /* 
  172.              * 成功输出正确的文件名 
  173.              */  
  174.             System.out.println(“You chose to open this file: “ + file.getName());  
  175.         } else {  
  176.             /* 
  177.              * 直接提供参数说明          *  
  178.              */  
  179.             file = new File(args[0]);  
  180.         }  
  181.         /* 
  182.          * 最后验证file是否存在 
  183.          * 如果不存在则显示抛出异常 
  184.          */  
  185.         if (!file.exists()) {  
  186.             throw new FileNotFoundException(file.getAbsolutePath());  
  187.         }  
  188.         return file;  
  189.     }  
  190. }  

/*
* 判断是够选择了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;
}

 

程序运行结果:

 

 

 

 

转载自:https://blog.csdn.net/mike_caoyong/article/details/7913849

You may also like...

退出移动版