选择一个版本比如:GeoTools 2.5 Releases,然后下载geotools-2.5.8-src.zip和geotools-2.5.8-bin.zip文件
具体的环境搭建参照:http://hunterlid.iteye.com/blog/
Geotools 使用说明
http://docs.codehaus.org/display/GEOTDOC/Home
OpenGIS 软件架构
org.geotools.data
包负责地理数据的读写(如:ShavefileReader用于读取shpfile数据),org.geotools.geometry
包负责提供对JTs的调用接口,以将地理数据封装成JTS中定义的几何对象(Geometry),
org.geotools.feature包负责封装空间几何要素对象(Feature),对应于地图中一个实体,
包含:空间数据(Geometry)、属性数据(Aitribute)、参考坐标系(Refereneedsystem)、
最小外包矩形(EnveloPe)等属性,是Gls操作的核心数据模型。
- package com.mapbar.geo.data;
- import java.io.File;
- import java.io.IOException;
- import java.io.Serializable;
- import java.net.MalformedURLException;
- import java.nio.charset.Charset;
- import java.util.Collection;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.Map;
- import org.geotools.data.DataUtilities;
- import org.geotools.data.DefaultTransaction;
- import org.geotools.data.FeatureSource;
- import org.geotools.data.Transaction;
- import org.geotools.data.shapefile.ShapefileDataStore;
- import org.geotools.data.shapefile.ShapefileDataStoreFactory;
- import org.geotools.data.shapefile.ShapefileFeatureLocking;
- import org.geotools.data.shapefile.ShpFiles;
- import org.geotools.data.shapefile.dbf.DbaseFileHeader;
- import org.geotools.data.shapefile.dbf.DbaseFileReader;
- import org.geotools.feature.FeatureCollection;
- import org.geotools.feature.FeatureCollections;
- import org.geotools.feature.FeatureIterator;
- import org.geotools.feature.simple.SimpleFeatureBuilder;
- import org.geotools.referencing.crs.DefaultGeographicCRS;
- import org.opengis.feature.Property;
- import org.opengis.feature.simple.SimpleFeature;
- import org.opengis.feature.simple.SimpleFeatureType;
- import com.vividsolutions.jts.geom.Coordinate;
- import com.vividsolutions.jts.geom.GeometryFactory;
- import com.vividsolutions.jts.geom.MultiLineString;
- import com.vividsolutions.jts.geom.Point;
- /
- *
- * Class MapbarReader.java
- *
- * Description Geotools shp格式文件的读取,创建,和写入操作
- *
- * Company mapbar
- *
- * author Chenll E-mail:
- *
- * Version 1.0
- *
- * Date 2012-2-9 下午03:31:51
- */
- public class MapbarReader {
- /
- * 读取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();
- // 迭代读取记录
- 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) {
- }
- }
- }
- }
- /
- * 读取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();
- FeatureIterator<SimpleFeature> itertor = result.features();
- while (itertor.hasNext()) {
- SimpleFeature feature = itertor.next();
- Collection<Property> p = feature.getProperties();
- Iterator<Property> it = p.iterator();
- while (it.hasNext()) {
- Property pro = it.next();
- // 坐标属性
- if (pro.getValue() instanceof MultiLineString) {
- // 坐标信息
- MultiLineString line = (MultiLineString) pro.getValue();
- // 总坐标个数
- int pointNums = line.getNumPoints();
- // 中心点坐标
- double centX = line.getCentroid().getX();
- double centY = line.getCentroid().getY();
- } else {
- // 其它属性以及值
- String name = pro.getName().toString();
- String value = pro.getValue().toString();
- }
- }
- }
- itertor.close();
- } catch (MalformedURLException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- /
- * 创建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.");
- double longitude = Double.parseDouble("39.");
- 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);
- // 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();
- }
- }
- public static void main(String[] args) {
- String DBFinput = "D:\\安康公交线_polyline.dbf";
- String shapinput = "D:\\安康公交线_polyline.shp";
- MapbarReader mr = new MapbarReader();
- String outShp = "D:\\busStation.shp";
- mr.createShp(outShp);
- }
- }
package com.mapbar.geo.data; import java.io.File; import java.io.IOException; import java.io.Serializable; import java.net.MalformedURLException; import java.nio.charset.Charset; import java.util.Collection; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import org.geotools.data.DataUtilities; import org.geotools.data.DefaultTransaction; import org.geotools.data.FeatureSource; import org.geotools.data.Transaction; import org.geotools.data.shapefile.ShapefileDataStore; import org.geotools.data.shapefile.ShapefileDataStoreFactory; import org.geotools.data.shapefile.ShapefileFeatureLocking; import org.geotools.data.shapefile.ShpFiles; import org.geotools.data.shapefile.dbf.DbaseFileHeader; import org.geotools.data.shapefile.dbf.DbaseFileReader; import org.geotools.feature.FeatureCollection; import org.geotools.feature.FeatureCollections; import org.geotools.feature.FeatureIterator; import org.geotools.feature.simple.SimpleFeatureBuilder; import org.geotools.referencing.crs.DefaultGeographicCRS; import org.opengis.feature.Property; import org.opengis.feature.simple.SimpleFeature; import org.opengis.feature.simple.SimpleFeatureType; import com.vividsolutions.jts.geom.Coordinate; import com.vividsolutions.jts.geom.GeometryFactory; import com.vividsolutions.jts.geom.MultiLineString; import com.vividsolutions.jts.geom.Point; / * * Class MapbarReader.java * * Description Geotools shp格式文件的读取,创建,和写入操作 * * Company mapbar * * author Chenll E-mail: * * Version 1.0 * * Date 2012-2-9 下午03:31:51 */ public class MapbarReader { / * 读取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(); // 迭代读取记录 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) { } } } } / * 读取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(); FeatureIterator<SimpleFeature> itertor = result.features(); while (itertor.hasNext()) { SimpleFeature feature = itertor.next(); Collection<Property> p = feature.getProperties(); Iterator<Property> it = p.iterator(); while (it.hasNext()) { Property pro = it.next(); // 坐标属性 if (pro.getValue() instanceof MultiLineString) { // 坐标信息 MultiLineString line = (MultiLineString) pro.getValue(); // 总坐标个数 int pointNums = line.getNumPoints(); // 中心点坐标 double centX = line.getCentroid().getX(); double centY = line.getCentroid().getY(); } else { // 其它属性以及值 String name = pro.getName().toString(); String value = pro.getValue().toString(); } } } itertor.close(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } / * 创建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."); double longitude = Double.parseDouble("39."); 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); // 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(); } } public static void main(String[] args) { String DBFinput = "D:\\安康公交线_polyline.dbf"; String shapinput = "D:\\安康公交线_polyline.shp"; MapbarReader mr = new MapbarReader(); String outShp = "D:\\busStation.shp"; mr.createShp(outShp); } }
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");
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 );
Feature[] array; .... return DataUtilties.collection( array );
(4) DataUtilities.reader 格式化
convert a perfectly good collection to FeatureReader format.
- FeatureCollection collection;
- FeatureReader reader = DataUtilities.reader( collection );
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/bian-cheng-ji-chu/83755.html