gpt4 book ai didi

java - 未为 null CoordinateReferenceSystem 生成 PRJ 文件

转载 作者:太空宇宙 更新时间:2023-11-04 13:57:52 24 4
gpt4 key购买 nike

我正在尝试从文本文件创建形状文件。为此,我使用了 Geotools 库。它可以创建 shapefile,但我收到了关于我正在设置的坐标系为空的警告!

数据位于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;
}
}

最佳答案

请更改

final SimpleFeatureType TYPE = DataUtilities.createType("Location", "location:Point");

final SimpleFeatureType TYPE = DataUtilities.createFeatureType();

或请更改

final SimpleFeatureType TYPE = DataUtilities.createType("Location",   "location:Point");

final SimpleFeatureType TYPE = DataUtilities.createType("Location",
"location:Point:srid=4326," +
"name:String," +
"number:Integer"
);

并删除方法CreateFeatureType()

关于java - 未为 null CoordinateReferenceSystem 生成 PRJ 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29632294/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com