package ghgf;
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: Chenll@mapbar.com
*
* Version 1.0
*
* Date 2012-2-9 下午03:31:51
*/
public class ffg {
/**
* 读取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();
String vvv = "ffff";
}
}
}
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.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);
// 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];
System.out.println(typeName);
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:\\华微\\中国详细shapefile文件\\地级城市驻地.shp";
ffg mr = new ffg();
String outShp = "D:\\busStation.shp";
mr.readSHP(shapinput);
mr.createShp(outShp);
}
}
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: Chenll@mapbar.com
*
* Version 1.0
*
* Date 2012-2-9 下午03:31:51
*/
public class ffg {
/**
* 读取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();
String vvv = "ffff";
}
}
}
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.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);
// 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];
System.out.println(typeName);
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:\\华微\\中国详细shapefile文件\\地级城市驻地.shp";
ffg mr = new ffg();
String outShp = "D:\\busStation.shp";
mr.readSHP(shapinput);
mr.createShp(outShp);
}
}