标签:des blog class code java tar
转自: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 数据格式的例子:
-
-
-
-
-
- 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();
- List<AttributeDescriptor> columns = schema
- .getAttributeDescriptors();
- FeatureIterator<SimpleFeature> itertor = result.features();
-
-
-
-
-
- while (itertor.hasNext()) {
- SimpleFeature feature = itertor.next();
- for (AttributeDescriptor attributeDes : columns) {
- String attributeName = attributeDes.getName().toString();
- if (attributeName.equals("the_geom"))
- continue;
- feature.getAttribute(attributeName);
- }
- Geometry g = (Geometry) feature.getDefaultGeometry();
- }
- itertor.close();
- } catch (MalformedURLException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
-
-
-
-
- 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);
- 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();
- String info = value.toString();
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- } catch (Exception ex) {
- ex.printStackTrace();
- } finally {
- if (reader != null) {
-
- try {
- reader.close();
- } catch (Exception e) {
- }
- }
- }
- }
输出一个shp文件
-
-
-
-
-
- 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 };
-
- SimpleFeature feature = featureBuilder.buildFeature(null, obj);
-
- collection.add(feature);
-
- 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,布布扣,bubuko.com
GeoTools应用-DATA
标签:des blog class code java tar
原文地址:http://www.cnblogs.com/cugwx/p/3719282.html