Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I am trying to merge some shapefiles with different schemas in geotools. Im aware of ogr2ogr, but for now I would like to figure out this problem with Geotools.

The steps I'm taking are:

1) Create a new Merged shapefile with the schema from the first shapefile in a list 2) Update the schema of the Merged shapefile file with the subsequent shapefiles in the list of shapefiles 3) Add features to the merged shapefile 4) Write the shapefile

The problem is, I cant add features from shapefiles that contain different schemas. Ok, that seems straight forward enough.

But when I try to get the schema from a subsequent shapefile and update the Merged shapefile schema, Geotools throws an exception:

"Schema modification not supported"

Why wont geotools let me update the schema? Is there something more than just simply calling

mergedData.updateSchema(shapefileData.getSchema())

public static void init(ArrayList<String> shpArray, String sessionName)
{
    Config config = new Config();

    String sessionData = config.getProperty("pathTo_fade_servlets_data") + "sessions/" + sessionName;
    String newUrl = sessionData + "/tempShp.shp";

    init(newUrl, shpArray.get(0));

    for(int k = 1; k < shpArray.size(); k++)
    {
        mergeShapefile(shpArray.get(k));
    }

    saveShapefile();
}

@SuppressWarnings("rawtypes")
public static void init(String newURL, String copyURL)
{
    try 
    {

        File newFile = new File(newURL);

        Map<String, Serializable> newFileParams = new HashMap<String, Serializable>();
        newFileParams.put("url", newFile.toURI().toURL());

        ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();

        mergedData = (ShapefileDataStore) dataStoreFactory.createNewDataStore(newFileParams);

        //

        File copyFile = new File(copyURL);

        Map<String, Serializable> copyFileParams = new HashMap<String, Serializable>();
        copyFileParams.put("url", copyFile.toURI().toURL());

        ShapefileDataStore shapefileData = (ShapefileDataStore) dataStoreFactory.createNewDataStore(copyFileParams);

        mergedData.createSchema(shapefileData.getSchema());

        mergedFeatureStore = (FeatureStore)mergedData.getFeatureSource(shapefileData.getTypeNames()[0]);

        mergedTransaction = mergedFeatureStore.getTransaction();

    } 
    catch (MalformedURLException e) 
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 
    catch (IOException e) 
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }        
}

@SuppressWarnings({ "rawtypes", "unchecked" })
private synchronized static void mergeShapefile(String url) 
{
    if (url == null) return;

    try 
    {
        File file = new File(url);

        Map<String, Serializable> fileParams = new HashMap<String, Serializable>();
        fileParams.put("url", file.toURI().toURL());

        ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();

        //

        ShapefileDataStore shapefileData = (ShapefileDataStore) dataStoreFactory.createNewDataStore(fileParams);

        FeatureCollection features = shapefileData.getFeatureSource().getFeatures();
        //error occurs here:
        mergedData.updateSchema(shapefileData.getSchema());

        mergedFeatureStore.addFeatures(features);

    } 
    catch (Exception e) 
    {
        e.printStackTrace();
    }
}

 private static void saveShapefile() 
 {
        if (mergedTransaction == null) return;

        try 
        { 

            mergedTransaction.commit();

            mergedTransaction.close();

        } 
        catch (Exception e) 
        {
            e.printStackTrace();
        }
    }
share|improve this question
add comment

2 Answers

Shapefiles in GeoTools are not mutable - the only way to change the schema of the shapefile is to read it in and write it back out to a new file with the modified schema.

To merge heterogeneous shapefiles you will need to read the schema of each file you want to import and then create a new schema that contains each attribute in those schemas. Then for each file read in the features and convert them to the new schema and write it back out to the new file.

share|improve this answer
add comment
up vote 1 down vote accepted

I figured out how to do this. An outline of the steps involved are as follows:

  • Get the schema of all the feature classes (shapefiles) getting merged
  • Combine these schemas into a new schema Read each feature for each
  • feature class and copy the geometry and attributes to the new feature
  • class (shapefile) Write the shapefile

Coming from an ArcObjects background, I thought there would be a way Geotools would create the new schema and copy the attributes over for you, but we have to write this code ourselves. It was very simple to this though. In AO, that is all done behind the scenes with the IBasicGeoprocessor interface.

Thanks for the reply. I actually solved this before I read the response :)

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.