GeoTools应用-DATA
转自:http://blog.csdn.net/cdl2008sky/article/details/7266785
一、Geotools The Open Source Java GIS Toolkit
http://geotools.org/ Geotools官方网站
http://docs.geotools.org/latest/javadocs/ Geotools API在线文档
http://docs.codehaus.org/display/GEOTDOC/Home Geotools用户指南
http://repo.opengeo.org Geotools的maven仓库地址
http://download.osgeo.org/webdav/geotools/ maven仓库地址
POM.xml配置
- <repositories>
- <repository>
- <id>osgeo</id>
- <name>Open Source Geospatial Foundation Repository</name>
- <url>http://download.osgeo.org/webdav/geotools/</url>
- </repository>
- <repository>
- <snapshots>
- <enabled>true</enabled>
- </snapshots>
- <id>opengeo</id>
- <name>OpenGeo Maven Repository</name>
- <url>http://repo.opengeo.org</url>
- </repository>
- </repositories>
eg:取到gt-main.jar的依赖关系
- <dependency>
- <groupId>org.geotools</groupId>
- <artifactId>gt-main</artifactId>
- <version>8.4</version>
- </dependency>
二、OpenGIS 软件架构

org.geotools.data
包负责地理数据的读写(如:ShavefileReader用于读取shpfile数据),org.geotools.geometry
包负责提供对JTs的调用接口,以将地理数据封装成JTS中定义的几何对象(Geometry),
org.geotools.feature包负责封装空间几何要素对象(Feature),对应于地图中一个实体,
包含:空间数据(Geometry)、属性数据(Aitribute)、参考坐标系(Refereneedsystem)、
最小外包矩形(EnveloPe)等属性,是Gls操作的核心数据模型。
Geotools 读取shp 数据格式的例子:
- /**
- * 读取shap格式的文件
- *
- * @param path
- */
- public void readSHP(String path) {
- ShapefileDataStore shpDataStore = null;
- try {
- shpDataStore = new ShapefileDataStore(new File(path).toURI()
- .toURL());
- shpDataStore.setStringCharset(Charset.forName("GBK"));
- // 文件名称
- String typeName = shpDataStore.getTypeNames()[0];
- FeatureSource<SimpleFeatureType, SimpleFeature> featureSource = null;
- featureSource = (FeatureSource<SimpleFeatureType, SimpleFeature>) shpDataStore
- .getFeatureSource(typeName);
- FeatureCollection<SimpleFeatureType, SimpleFeature> result = featureSource
- .getFeatures();
- SimpleFeatureType schema = result.getSchema(); // schema
- List<AttributeDescriptor> columns = schema
- .getAttributeDescriptors();
- FeatureIterator<SimpleFeature> itertor = result.features();
- /*
- * 或者使用 FeatureReader FeatureReader reader =
- * DataUtilities.reader(result); while(reader.hasNext()){
- * SimpleFeature feature = (SimpleFeature) reader.next(); }
- */
- while (itertor.hasNext()) {
- SimpleFeature feature = itertor.next();
- for (AttributeDescriptor attributeDes : columns) {
- String attributeName = attributeDes.getName().toString();// attribute
- if (attributeName.equals("the_geom"))
- continue;
- feature.getAttribute(attributeName); // attributeValue
- }
- Geometry g = (Geometry) feature.getDefaultGeometry();// Geometry
- }
- itertor.close();
- } catch (MalformedURLException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- /**
- * 读取dbf格式的文件,只存储属性值,不存储空间值
- *
- * @param path
- */
- public void readDBF(String path) {
- DbaseFileReader reader = null;
- try {
- reader = new DbaseFileReader(new ShpFiles(path), false,
- Charset.forName("GBK"));
- DbaseFileHeader header = reader.getHeader();
- int numFields = header.getNumFields();
- for (int i = 0; i < numFields; i++) {
- header.getFieldName(i);
- header.getFieldType(i);// 'C','N'
- header.getFieldLength(i);
- }
- // 迭代读取记录
- while (reader.hasNext()) {
- try {
- Object[] entry = reader.readEntry();
- for (int i = 0; i < numFields; i++) {
- String title = header.getFieldName(i);
- Object value = entry[i];
- String name = title.toString(); // column
- String info = value.toString(); // value
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- } catch (Exception ex) {
- ex.printStackTrace();
- } finally {
- if (reader != null) {
- // 关闭
- try {
- reader.close();
- } catch (Exception e) {
- }
- }
- }
- }
输出一个shp文件
- /**
- * 创建shp文件
- *
- * @param outPath
- */
- public void createShp(String outPath) {
- try {
- // 定义属性
- final SimpleFeatureType TYPE = DataUtilities.createType("Location",
- "location:Point," + "NAME:String," + "INFO:String,"
- + "OWNER:String");
- FeatureCollection<SimpleFeatureType, SimpleFeature> collection = FeatureCollections.newCollection();
- GeometryFactory geometryFactory = new GeometryFactory();
- SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(TYPE);
- double latitude = Double.parseDouble("116.123456789");
- double longitude = Double.parseDouble("39.120001");
- String NAME = "运通110路";
- String INFO = "白班车,学生票有效";
- String OWNER = "001";
- //创建坐标
- Point point = geometryFactory.createPoint(new Coordinate(longitude,latitude));
- //创建属性值
- Object[] obj = {point, NAME, INFO, OWNER };
- //构造一个Feature
- SimpleFeature feature = featureBuilder.buildFeature(null, obj);
- //添加到集合
- collection.add(feature);
- // shap文件的输出路径
- File newFile = new File(outPath);
- Map<String, Serializable> params = new HashMap<String, Serializable>();
- params.put("url", (Serializable) newFile.toURI().toURL());
- params.put("create spatial index", (Serializable) Boolean.TRUE);
- ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();
- ShapefileDataStore newDataStore = (ShapefileDataStore) dataStoreFactory
- .createNewDataStore(params);
- newDataStore.createSchema(TYPE);
- newDataStore.setStringCharset(Charset.forName("GBK"));
- newDataStore.forceSchemaCRS(DefaultGeographicCRS.WGS84);
- String typeName = newDataStore.getTypeNames()[0];
- ShapefileFeatureLocking featureSource = (ShapefileFeatureLocking) newDataStore
- .getFeatureSource(typeName);
- // 创建一个事务
- Transaction transaction = new DefaultTransaction("create");
- featureSource.setTransaction(transaction);
- try {
- featureSource.addFeatures(collection);
- // 提交事务
- transaction.commit();
- } catch (Exception problem) {
- problem.printStackTrace();
- transaction.rollback();
- } finally {
- transaction.close();
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
org.geotools.data.DataUtilities
a facade classes which can help simplify common data wrangling chores 简化繁琐的通用数据
(1)、定义属性
FeatureType TYPE = DataUtilities.createType("Location",
"location:Point," + "NAME:String," + "INFO:String,"+ "OWNER:String");
(2) DataUtilities.schema
You can use this method to quickly get a representation of a FeatureType 返回FeatureType的schema
//返回schema
DataUtilities.spec(featureType))
(3) DataUtilities.collection Feature数组转换为Feature集合
DataUtilities has helper methods to turn almost anything into a FeatureCollection
Feature[] array;
....
return DataUtilties.collection( array );
(4) DataUtilities.reader 格式化
convert a perfectly good collection to FeatureReader format.
FeatureCollection collection;
FeatureReader reader = DataUtilities.reader( collection );
附:shp 格式文件介绍
Shapefile file extensions
.shp—The main file that stores the feature geometry. Required.
.shx—The index file that stores the index of the feature geometry. Required.
.dbf—The dBASE table that stores the attribute information of features. Required.There is a one-to-one relationship between geometry and attributes, which is based on record number.
.prj—The file that stores the coordinate system information. Used by ArcGIS.
DBF文件中的数据类型FieldType
代码 数据类型 允许输入的数据
B 二进制型 各种字符。
C 字符型 各种字符。
D 日期型 用于区分年、月、日的数字和一个字符,内部存储按照YYYYMMDD格式。
G (Generalor OLE) 各种字符。
N 数值型(Numeric) - . 0 1 2 3 4 5 6 7 8 9
L 逻辑型(Logical)? Y y N n T t F f (? 表示没有初始化)。
M (Memo) 各种字符。
GeoTools应用-DATA的更多相关文章
- GeoTools介绍、环境安装、读取shp文件并显示
GeoTools是一个开放源代码(LGPL)Java代码库,它提供了符合标准的方法来处理地理空间数据,例如实现地理信息系统(GIS).GeoTools库实现了开放地理空间联盟(OGC)规范. Geot ...
- Spring-Boot ☞ ShapeFile文件读写工具类+接口调用
一.项目目录结构树 二.项目启动 三.往指定的shp文件里写内容 (1) json数据[Post] { "name":"test", "path&qu ...
- JAVA用geotools读写shape格式文件
转自:http://toplchx.iteye.com/blog/1335007 JAVA用geotools读写shape格式文件 (对应geotools版本:2.7.2) (后面添加对应geotoo ...
- geotools导入shp文件到Oracle数据库时表名带下划线的问题解决
问题: 最近在做利用geotools导入shp文件到Oracle表中,发现一个问题Oracle表名带下划线时导入失败,问题代码行: dsOracle.getFeatureWriterAppend(or ...
- maven构建geotools应用工程
前置条件 jdk1.7+eclipse+maven POM配置 <project xmlns="http://maven.apache.org/POM/4.0.0" xmln ...
- 简析服务端通过geotools导入SHP至PG的方法
文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/ 1.背景 项目中需要在浏览器端直接上传SHP后服务端进行数据的自动入PG ...
- geotools中泰森多边形的生成
概述 本文讲述如何在geotools中生成泰森多边形,并shp输出. 泰森多边形 1.定义 泰森多边形又叫冯洛诺伊图(Voronoi diagram),得名于Georgy Voronoi,是由一组由连 ...
- 说说geotools中坐标转换那点事
概述: 本文说说geotools中坐标转换的那点事情,以WGS84和web墨卡托相互转换为例. 效果: 转换前 转换后 单个Geometry转换 实现代码: package com.lzugis.ge ...
- geotools修改shapefile 属性名乱码问题
在GeoServer中文社区的讨论地址为:http://opengeo.cn/bbs/read.php?tid=1701&page=e&#a 使用geotools修改shapefile ...
随机推荐
- 自动构建工具Ant的使用-笔记
第一:什么是Ant? Apache Ant是一个基于Java的生成工具.据最初的创始人James Duncan Davidson的介绍,这个工具的名称是another neat tool(另一个整洁的 ...
- 销毁session
session运行在服务器是单用户,每个session都有一个唯一的sessionid 用法:session.setAttribute("userName", "张三丰& ...
- (zzuli)1907 小火山的宝藏收益
Description 进去宝藏后, 小火山发现宝藏有N个房间,且这n个房间通过N-1道门联通. 每一个房间都有一个价值为Ai的宝藏, 但是每一个房间也都存在一个机关.如果小火山取走了这个房间的宝藏, ...
- c++ primer复习(三)
1 istream.ostream类型,cin.cout.cerr是istream或ostream类型的具体的对象,<<和>>是操纵符 getline函数的参数是istream ...
- Scala - 处理时间(nscala-time - Joda Time的scala封装)
GITHUB : https://github.com/nscala-time/nscala-time MAVEN : (注意选对scala版本) <dependency> <gro ...
- .NET多线程编程(转)
在.NET多线程编程这个系列我们讲一起来探讨多线程编程的各个方面.首先我将在本篇文章的开始向大家介绍多线程的有关概念以及多线程编程的基础知识;在接下来的文章中,我将逐一讲述.NET平台上多线程编程的知 ...
- css动画怎么写:3个属性实现
3个属性:transition,animation,transform 实现步骤: 1.css定位 2.rgba设置颜色透明度 3.转换+动画 transform+animation 4.动画平滑过渡 ...
- 表格table样式布局设置
<style> table{ border-collapse:collapse; margin:0 auto;} table tr td{ border:1px solid #000; l ...
- 使用GetLogicalDriveStrings获取驱动器根路径
使用GetLogicalDriveStrings获取驱动器根路径,并使用自定义的GetDriveInfo函数获取驱动器的属性. VS2012 + win7 x64下调试通过. #include < ...
- POJ 1129 Channel Allocation 四色定理dfs
题目: http://poj.org/problem?id=1129 开始没读懂题,看discuss的做法,都是循环枚举的,很麻烦.然后我就决定dfs,调试了半天终于0ms A了. #include ...