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;
com.vividsolutions.jts.geom.GeometryFactory Import;
Import com.vividsolutions.jts.geom.MultiLineString;
Import com.vividsolutions.jts.geom.Point;
/ **
*
* Class MapbarReader.java
*
* SHP format of the Description Geotools reading, creating, and writing
*
* Company Mapbar
*
* E-mail author Chenll: [email protected]
*
* Version 1.0
*
* a Date 2012-2-9 03:31:51 PM
* /
public class FFG {
/ **
* read dbf file format
*
* @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();
Value.toString info = String ();
}
} the catch (Exception E) {
e.printStackTrace ();
}
}
} the catch (Exception EX) {
ex.printStackTrace ();
} the finally {
IF (! Reader = null) {
/ / off
the try {
reader.Close ();
} the catch (Exception E) {
}
}
}
}
/ **
* reads shap format files
*
* @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) {
// 坐标信息
Line = a MultiLineString (a MultiLineString) pro.getValue ();
// total number of coordinate
int pointNums line.getNumPoints = ();
// center coordinate
Double centX = line.getCentroid () getX ();.
Double centY = Line. . getCentroid () getY ();
} the else {
// attributes and values of other
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);
// commit the transaction
transaction.commit ();
} the catch (Exception problem) {
problem.printStackTrace ();
Transaction.rollback ();
} {the finally
transaction.close ();
}
} the catch (Exception E) {
e.printStackTrace ();
}
}
public static void main (String [] args) {
String DBFinput = "D: \\ well-bus lines _polyline.dbf";
String shapinput = "D: \\, micro \ \ China detailed the shapefile \\ prefecture-level city resident .shp ";
FFG mr = new 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;
com.vividsolutions.jts.geom.GeometryFactory Import;
Import com.vividsolutions.jts.geom.MultiLineString;
Import com.vividsolutions.jts.geom.Point;
/ **
*
* Class MapbarReader.java
*
* SHP format of the Description Geotools reading, creating, and writing
*
* Company Mapbar
*
* E-mail author Chenll: [email protected]
*
* Version 1.0
*
* a Date 2012-2-9 03:31:51 PM
* /
public class FFG {
/ **
* read dbf file format
*
* @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();
Value.toString info = String ();
}
} the catch (Exception E) {
e.printStackTrace ();
}
}
} the catch (Exception EX) {
ex.printStackTrace ();
} the finally {
IF (! Reader = null) {
/ / off
the try {
reader.Close ();
} the catch (Exception E) {
}
}
}
}
/ **
* reads shap format files
*
* @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) {
// 坐标信息
Line = a MultiLineString (a MultiLineString) pro.getValue ();
// total number of coordinate
int pointNums line.getNumPoints = ();
// center coordinate
Double centX = line.getCentroid () getX ();.
Double centY = Line. . getCentroid () getY ();
} the else {
// attributes and values of other
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);
// commit the transaction
transaction.commit ();
} the catch (Exception problem) {
problem.printStackTrace ();
Transaction.rollback ();
} {the finally
transaction.close ();
}
} the catch (Exception E) {
e.printStackTrace ();
}
}
public static void main (String [] args) {
String DBFinput = "D: \\ well-bus lines _polyline.dbf";
String shapinput = "D: \\, micro \ \ China detailed the shapefile \\ prefecture-level city resident .shp ";
FFG mr = new new FFG ();
String outShp = "D:\\busStation.shp";
mr.readSHP(shapinput);
mr.createShp(outShp);
}
}