用java语言创建shapefile文件,是从txt格式文件创建为shapefile,主要是Geo Tools中的类库,类库下载地址:http://geotools.org/
/*
* TrajectoryMinerTools Module
*
* Copyright (C) 2002-2011,Institute of Geographic Sciences and Natural Resources Research,
* Chinese Academy of Sciences
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation;
* version 2.1 of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*/
package org.cas.mo.tools.gis.geotools;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.geotools.data.DataUtilities;
import org.geotools.data.DefaultTransaction;
import org.geotools.data.Transaction;
import org.geotools.data.collection.ListFeatureCollection;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.shapefile.ShapefileDataStoreFactory;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.data.simple.SimpleFeatureStore;
import org.geotools.feature.FeatureCollections;
import org.geotools.feature.simple.SimpleFeatureBuilder;
import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
import org.geotools.geometry.jts.JTSFactoryFinder;
import org.geotools.referencing.crs.DefaultGeographicCRS;
import org.geotools.swing.data.JFileDataStoreChooser;
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.Point;
public class Text2Shapefile {
public static void readTxtToShapefile(String txtname, String shpname)
throws Exception {
File file = new File(txtname);
final SimpleFeatureType TYPE = DataUtilities.createType("Location",
"location:Point:srid=4326," + "time:String," + "trajid:String");
List<SimpleFeature> features = new ArrayList<SimpleFeature>();
//SimpleFeatureCollection collection = FeatureCollections.newCollection();
GeometryFactory geometryFactory = JTSFactoryFinder
.getGeometryFactory(null);
SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(TYPE);
BufferedReader reader = new BufferedReader(new FileReader(file));
try {
String line = reader.readLine();
System.out.println("Header: " + line);
for (line = reader.readLine(); line != null; line = reader
.readLine()) {
if (line.trim().length() > 0) { // skip blank lines
String tokens[] = line.split(",");
double longitude = Double.parseDouble(tokens[0]);
double latitude = Double.parseDouble(tokens[1]);
String time = tokens[2].trim();
String trajid = tokens[3].trim();
Point point = geometryFactory.createPoint(new Coordinate(
longitude, latitude));
featureBuilder.add(point);
featureBuilder.add(time);
featureBuilder.add(trajid);
SimpleFeature feature = featureBuilder.buildFeature(null);
//((SimpleFeatureBuilder) collection).add(feature);
features.add(feature);
}
}
} finally {
reader.close();
}
File newFile = new File(shpname);
ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();
Map<String, Serializable> params = new HashMap<String, Serializable>();
params.put("url", newFile.toURI().toURL());
params.put("create spatial index", Boolean.TRUE);
ShapefileDataStore newDataStore = (ShapefileDataStore) dataStoreFactory
.createNewDataStore(params);
newDataStore.createSchema(TYPE);
newDataStore.forceSchemaCRS(DefaultGeographicCRS.WGS84);
Transaction transaction = new DefaultTransaction("create");
String typeName = newDataStore.getTypeNames()[0];
SimpleFeatureSource featureSource = newDataStore
.getFeatureSource(typeName);
if (featureSource instanceof SimpleFeatureStore) {
SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;
SimpleFeatureCollection collection = new ListFeatureCollection(TYPE, features);
featureStore.setTransaction(transaction);
try {
featureStore.addFeatures(collection);
transaction.commit();
} catch (Exception problem) {
problem.printStackTrace();
transaction.rollback();
} finally {
transaction.close();
}
System.exit(0); // success!
} else {
System.out
.println(typeName + " does not support read/write access");
System.exit(1);
}
}
}