注意:运行 geotools 13.0.
我有一个创建点形状的应用程序。然后,我的应用程序将这些功能写入 shapefile,然后写入 MySQL。
当我在 Windows 下运行代码时,一切正常:数据正确存储在 MySQL 中,我可以毫无问题地使用它。
当我在 Linux 下运行代码时,创建了 shapefile,但数据没有写入 MySQL,并引发以下异常:
WARNING: class org.geotools.filter.function.Collection_NearestFunction has name conflict betwee 'null' and 'Collection_Nearest'
Exception in thread "main" java.lang.NoSuchFieldError: LINEARIZATION_TOLERANCE
at org.geotools.jdbc.JDBCFeatureReader.init(JDBCFeatureReader.java:211)
at org.geotools.jdbc.JDBCFeatureReader.<init>(JDBCFeatureReader.java:137)
at org.geotools.jdbc.JDBCInsertFeatureWriter.<init>(JDBCInsertFeatureWriter.java:43)
at org.geotools.jdbc.JDBCFeatureStore.getWriterInternal(JDBCFeatureStore.java:280)
at org.geotools.data.store.ContentFeatureStore.getWriter(ContentFeatureStore.java:151)
at org.geotools.data.store.ContentFeatureStore.getWriter(ContentFeatureStore.java:122)
at org.geotools.data.store.ContentFeatureStore.getWriterAppend(ContentFeatureStore.java:263)
at org.geotools.data.store.ContentFeatureStore.addFeatures(ContentFeatureStore.java:242)
at com.technip.projects.gis.GISGenerator.writeShapesMySQL(GISGenerator.java:763)
at com.technip.projects.gis.GISGenerator.generatePlatforms(GISGenerator.java:416)
at com.technip.projects.gis.GISGenerator.createShapefiles(GISGenerator.java:249)
at Machine.run(Machine.java:739)
at Machine.main(Machine.java:329)
我的代码:
private void writeShapesMySQL(List<SimpleFeature> features) throws IOException {
SimpleFeatureType TYPE = null;
if (!features.isEmpty()) {
TYPE = features.get(0).getType();
// Drop the table if exists
try (Connection con = conf.requestConnection("gis")) {
con.prepareCall("DROP TABLE IF EXISTS " + TYPE.getTypeName() + ";").execute();
} catch (Exception e) {}
if (factory == null) {
initMySQLStore();
}
SimpleFeatureCollection collection = new ListFeatureCollection(TYPE, features);
gisDS.createSchema(TYPE);
Transaction transaction = new DefaultTransaction("create");
String typeName = null;
for (String s : gisDS.getTypeNames()) {
if (s.equalsIgnoreCase(TYPE.getTypeName())) {
typeName = s;
break;
}
}
if (typeName == null) {
log.error("Cannot find the type " + TYPE.getTypeName() + " in the known types: " + String.valueOf(gisDS.getTypeNames()));
throw new IOException("Cannot find type " + TYPE.getTypeName() + " -- in other words, developer sucks.");
}
SimpleFeatureSource featureSource = gisDS.getFeatureSource(typeName);
// System.out.println("SHAPE:"+SHAPE_TYPE);
if (featureSource instanceof SimpleFeatureStore) {
SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;
featureStore.setTransaction(transaction);
try {
log.info("Adding " + collection.size() + " features into " + TYPE.getTypeName() + " table.");
featureStore.addFeatures(collection);
transaction.commit();
} catch (Exception problem) {
log.error("Cannot create shapes in MySQL for " + TYPE.getTypeName(), problem);
transaction.rollback();
} finally {
transaction.close();
}
}
} else {
log.warn("Passed empty list to create GIS database.");
}
}
private void initMySQLStore() throws IOException {
factory = new MySQLDataStoreFactory();
Map conMap = new HashMap();
conMap.put("dbtype", "mysql");
conMap.put("host", conf.getDbserver());
conMap.put("port", "3306");
conMap.put("database", "gis");
conMap.put("user", conf.getDbuser());
conMap.put("passwd", conf.getDbpass());
gisDS = factory.createDataStore(conMap);
Map<Class<?>, Integer> classMappings = gisDS.getClassToSqlTypeMappings();
classMappings.put(String.class, new Integer(Types.LONGVARCHAR));
}
我的第一个提示是 MySQL 在 Linux 中区分大小写,但在 Windows 中不区分大小写。所以我检查了在 Linux 和 Windows 中创建的表,它们都没有具有这样名称的字段(LINEARIZATION_TOLERANCE)。
有什么提示吗?
谢谢,
胡安
--更新:问题已解决--
原来我在 Linux 机器上有一个旧版本的 geotools 的旧 .jar。删除所有旧罐子解决了这个问题。