我已经将csv转换为shapefile并在地图上显示,这里的问题是我如何管理csv数据并删除行中的速度小于5,在相同的转换代码中可以做到这一点?
这是我的代码的一部分,我将csv转换为shapefile
public class Csv2shape {
public static void main(String[] args) throws Exception {
// Set cross-platform look & feel for compatability
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
File file = JFileDataStoreChooser.showOpenFile("csv", null);
if (file == null) {
return;
}
/*
* We use the DataUtilities class to create a FeatureType that will describe the data in our
* shapefile.
*
* See also the createFeatureType method below for another, more flexible approach.
*/
final SimpleFeatureType TYPE = DataUtilities.createType( "Location",
"the_geom:Point:srid=4326," + // <- the geometry attribute: Point type
"Date:String," + // <- a String attribute
"Heure:String," + // <- a String attribute
"latitude:String," + // <- a String attribute
"vitesse:Double" // a number attribute
);
System.out.println("TYPE:"+TYPE);
/* A list to collect features as we create them.
*/
List<SimpleFeature> features = new ArrayList<SimpleFeature>();
/*
* GeometryFactory will be used to create the geometry attribute of each feature,
* using a Point object for the location.
*/
GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory();
SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(TYPE);
BufferedReader reader = new BufferedReader(new FileReader(file));
try {
/* First line of the data file is the header */
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("\\,");
String name1 = tokens[0].trim();
String name2 = tokens[1].trim();
double latitude = Double.parseDouble(tokens[2]);
double longitude = Double.parseDouble(tokens[3]);
String name = tokens[4].trim();
String name4 = tokens[5].trim();
/* Longitude (= x coord) first ! */
Point point = geometryFactory.createPoint(new Coordinate(longitude, latitude));
featureBuilder.add(point);
featureBuilder.add(name1);
featureBuilder.add(name2);
featureBuilder.add(name);
featureBuilder.add(name4);
SimpleFeature feature = featureBuilder.buildFeature(null);
features.add(feature);
}
}
} finally {
reader.close();
}
/*
* Get an output file name and create the new shapefile
*/
/*
* Get an output file name and create the new shapefile
*/
File newFile = getNewShapeFile(file);
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);
/*
* TYPE is used as a template to describe the file contents
*/
newDataStore.createSchema(TYPE);
答案 0 :(得分:0)
如果我理解你的问题,你想做这样的事情:
double speed = Double.parseDouble(tokens[5].trim()); // fifth token is speed?
if(speed < 5) {
/* Longitude (= x coord) first ! */
Point point = geometryFactory.createPoint(new Coordinate(longitude, latitude));
/*add features .... */
}
答案 1 :(得分:0)
我很抱歉,如果我没有表达好,但我尽我所能来描述情况....在我的情况下,我想在第一个有速度&gt; = 5之后添加所有点,我发现如何做到这一点但问题是我必须从csv文件底部的顶部ans做同样的事情希望意味着该函数应该从底部再次开始
boolean foundAnyRowHigherThan5 = false;
for (line = reader.readLine(); line != null; line = reader.readLine()) {
if (line.trim().length() > 0) { // skip blank lines
String tokens[] = line.split("\\,");
String name1 = tokens[0].trim();
String name2 = tokens[1].trim();
double latitude = Double.parseDouble(tokens[2]);
double longitude = Double.parseDouble(tokens[3]);
String name = tokens[4].trim();
String speedString = tokens[5].trim();
float speedFloat = Float.parseFloat(speedString);
if(foundAnyRowHigherThan5 || speedFloat > 5.0) {
// from this point forward, you will not skip lines based on speed.
if(!foundAnyRowHigherThan5) {
foundAnyRowHigherThan5 = true;
}
/* Longitude (= x coord) first ! */
Point point = geometryFactory.createPoint(new Coordinate(longitude, latitude));
featureBuilder.add(point);
featureBuilder.add(name1);
featureBuilder.add(name2);
featureBuilder.add(name);
featureBuilder.add(speedString);
SimpleFeature feature = featureBuilder.buildFeature(null);
features.add(feature);
}
}
}
} finally {
reader.close();
}