我正在尝试从文本文件创建一个形状文件。为此,我使用了Geotools库。 它可以创建shapefile,但我收到一个关于坐标系的警告,该坐标系为null,我设置了它!
数据位于UTM-wgs84-Zone 30N。首先,我尝试了默认的坐标系统(WGS84),然后我使用了EPSG并对其进行了解码。它返回null。
public static void main(String[] args) throws Exception {
File[] files = getFiles("C://ArtCSVFiles//");
for (int i=0;i<files.length;i++) {
String outputFilePath = "C://Art//" +files[i].getName()+".shp";
//String inputFilePath = "C://ParkTxtFiles//ParkCluster0Mp10Dist0.005.csv";
String inputFilePath = files[i].getAbsolutePath();
final SimpleFeatureType TYPE = DataUtilities.createType("Location", "location:Point"); // see createFeatureType();
FeatureCollection<SimpleFeatureType, SimpleFeature> collection = FeatureCollections.newCollection();
BufferedReader reader = new BufferedReader(new FileReader(files[i]));
try {
GeometryFactory factory = JTSFactoryFinder.getGeometryFactory(null);
for (String line = reader.readLine(); line != null; line = reader.readLine()) {
String split[] = line.split("\\,");
double longitude = Double.parseDouble(split[0]);
double latitude = Double.parseDouble(split[1]);
Point point = factory.createPoint(new Coordinate(longitude, latitude));
SimpleFeature feature = SimpleFeatureBuilder.build(TYPE, new Object[]{point}, null);
collection.add(feature);
}
} finally {
reader.close();
}
File newFile = getNewShapeFile(files[i], outputFilePath);
DataStoreFactorySpi factory = new ShapefileDataStoreFactory();
Map<String, Serializable> create = new HashMap<String, Serializable>();
create.put("url", newFile.toURI().toURL());
create.put("create spatial index", Boolean.TRUE);
ShapefileDataStore newDataStore = (ShapefileDataStore) factory.createNewDataStore(create);
newDataStore.createSchema(TYPE);
newDataStore.forceSchemaCRS(DefaultGeographicCRS.WGS84);
Transaction transaction = new DefaultTransaction("create");
String typeName = newDataStore.getTypeNames()[0];
FeatureStore<SimpleFeatureType, SimpleFeature> featureStore;
featureStore = (FeatureStore<SimpleFeatureType, SimpleFeature>)
newDataStore.getFeatureSource(typeName);
featureStore.setTransaction(transaction);
try {
featureStore.addFeatures(collection);
transaction.commit();
} catch (Exception problem) {
problem.printStackTrace();
transaction.rollback();
} finally {
transaction.close();
}
//System.exit(0); // we are actually exiting because we will use a Swing JFileChooser
}
}
public static File[] getFiles(String args) {
return new File(args).listFiles();
}
private static File getNewShapeFile(File file, String outputFilePath) {
String path = file.getAbsolutePath();
String newPath = path.substring(0,path.length()-4)+".shp";
File newFile = new File(outputFilePath);
if( newFile.equals( file )){
System.out.println("Cannot replace "+file);
System.exit(0);
}
return newFile;
}
private static File getCSVFile(String[] args) throws FileNotFoundException {
File file;
if (args.length == 0){
JFileChooser chooser = new JFileChooser();
chooser.setDialogTitle("Open CSV file");
chooser.setFileFilter( new FileFilter(){
public boolean accept( File f ) {
return f.isDirectory() || f.getPath().endsWith("csv") || f.getPath().endsWith("CSV");
}
public String getDescription() {
return "Comma Seperated Value";
}
});
int returnVal = chooser.showOpenDialog( null );
if(returnVal != JFileChooser.APPROVE_OPTION) {
System.exit(0);
}
file = chooser.getSelectedFile();
System.out.println("Opening CVS file: " + file.getName());
}
else {
file = new File( args[0] );
}
if (!file.exists()){
throw new FileNotFoundException( file.getAbsolutePath() );
}
return file;
}
/**
* Here is how you can use a SimpleFeatureType build to create
* the schema for your shapefile dynamically.
* <p>
* This method is an improvement on the origional example as we
* are specifying DefaultGeographicCRS.WGS84 and a maximum field length.
* <p>
* @return SimpleFeatureType
*/
static SimpleFeatureType createFeatureType() throws FactoryException {
SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
builder.setName( "Location" );
CoordinateReferenceSystem crs = CRS.decode("EPSG:32630");
builder.setCRS(crs);
//add attributes in order
builder.add("Location", Point.class );
builder.length(15).add( "Name", String.class );
System.out.println(builder.crs(crs));
//build the type
final SimpleFeatureType LOCATION = builder.buildFeatureType();
return LOCATION;
}
}
答案 0 :(得分:0)
请更改
final SimpleFeatureType TYPE = DataUtilities.createType("Location", "location:Point");
到
final SimpleFeatureType TYPE = DataUtilities.createFeatureType();
OR 请改变
final SimpleFeatureType TYPE = DataUtilities.createType("Location", "location:Point");
到
final SimpleFeatureType TYPE = DataUtilities.createType("Location",
"location:Point:srid=4326," +
"name:String," +
"number:Integer"
);
并删除方法CreateFeatureType()