简体   繁体   中英

Geotools 13 - error writing to MySQL in linux

Note: Running geotools 13.0.

I have an application that creates Point shapes. My application then writes these features in a shapefile and then into MySQL.

When I run the code under Windows, everything works perfect: the data is properly stored in MySQL and I can use it without any trouble.

When I run the code under Linux, shapefiles are created, but the data is not written in MySQL and the following exception is thrown:

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)

My code:

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));
}

My first hint is that MySQL is case sensitive in Linux, but not in Windows. So I checked the created tables both in Linux and Windows, and none of them has a field with such a name (LINEARIZATION_TOLERANCE).

Any hints?

Thanks,

Juan

--UPDATE: PROBLEM SOLVED--

It turned out I had an old .jar from a prior version of geotools in the Linux machine. Removing all old jars fixed the problem.

Problem solved:

It turned out I had an old .jar from a prior version of geotools in the Linux machine. Removing all old jars fixed the problem.

Dismiss this, it is a user error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM