1

I have a shapefile (polygon features) which has seven attributes (id, area, etc.,) in the DBF file. After reading the shapefile I am exporting to a new shapefile in which I am adding two attributes (id ,area). But exported DBF file is bigger size than earlier one.

This is my simple feature type schema

    builder.setCRS(DefaultGeographicCRS.WGS84); 
    builder.setName(plotIdentifier);

    builder.add(sPolygon, Polygon.class);
    builder.add(plotIdentifier, String.class);
    builder.add(aRea,Float.class);

    final SimpleFeatureType featureType = builder.buildFeatureType();
    return new SimpleFeatureBuilder(featureType);

Please help me on this

Code creating polygon:

 public boolean addPolygonFeature(List<SimpleFeature> featureList, Point3D[] thePoints,
                                 String polyId, float area)
                                 throws Exception
{
    Polygon aPoly = createPolygon(thePoints);

    if (aPoly == null)
    {
        StringBuffer buf = new StringBuffer();
        buf.append(WError1);
        buf.append(polyId);
        buf.append(WError2);
        String error = buf.toString();

        int opt = GUIBase.showConfirmDialog(error, "Confirm", GUI_YN);
        return (opt == GUIBase.GUI_YES);
    }

    SimpleFeature feature = featureBuilder.buildFeature(null);
    feature.setAttribute(plotIdentifier, polyId.trim());
    feature.setAttribute(sPolygon, aPoly);
    feature.setAttribute(aRea, area);
    featureList.add(feature);
    return true;
}

public Polygon createPolygon(Point3D[] thePoints)
{
    int count = (thePoints == null) ? 0 : thePoints.length;
    if (count < 1) return null;

    // Store Coordinates of polygon as a LINESTRING.
    StringBuffer buffer = new StringBuffer();
    buffer.append("POLYGON((");

    for (int i=0; i<count; i++)
    {
        buffer.append(thePoints[i].x);
        buffer.append(SPACE);
        buffer.append(thePoints[i].y);
        buffer.append(COMMA);
    }

    String lineString = buffer.toString();
    lineString = lineString.substring(0, lineString.length()-1) + "))";
    if (DEBUG) System.out.println(lineString);

    try
    {
        // Creating the Geometry (Polygon)
        return (Polygon) wktReader.read(lineString); 
    }
    catch (Exception excep) { }

    return null;
}

public void writeShape(File theFile, List<SimpleFeature> featureList) throws Exception
{ 
    ShapefileDataStoreFactory dSFactory = new ShapefileDataStoreFactory();

    Map params = new HashMap();
    params.put("url", theFile.toURI().toURL());
    params.put("create spatial index", Boolean.TRUE);

    ShapefileDataStore newDataStore =
                 (ShapefileDataStore) dSFactory.createNewDataStore(params);

    newDataStore.createSchema(featureBuilder.getFeatureType());
    newDataStore.forceSchemaCRS(DefaultGeographicCRS.WGS84);

    String typeName = newDataStore.getTypeNames()[0];
    SimpleFeatureSource featureSource =
                                   newDataStore.getFeatureSource(typeName);
    if (featureSource instanceof SimpleFeatureStore) 
    {
        SimpleFeatureStore featureStore = (SimpleFeatureStore)featureSource;
        SimpleFeatureCollection collection =
                  new ListFeatureCollection(featureBuilder.getFeatureType(),
                                            featureList);
        Transaction transaction = new DefaultTransaction("create");
        featureStore.setTransaction(transaction);
        try
        {
            featureStore.addFeatures(collection);
            transaction.commit();
        }
        catch (Exception exp)
        {
            exp.printStackTrace();
            transaction.rollback();
        } 
        finally
        {
            transaction.close();
        }
    }
}

And shapefile link as given below:

https://docs.google.com/file/d/0B5Vh2tWJlQW_RURQaFBwaDJ6ckk/edit?usp=sharing

Read shapefile: 1.shp
Written shapefile: 2.shp
  • I think we need more code to help. Also what does the dbf file look like if you open it with a spreadsheet? – Ian Turton Oct 23 '13 at 8:00
  • if i open the dbf file it has 2 attributes and values and the values are correct ones. – Suri Oct 23 '13 at 9:06
3

When you write out your second shapefile you haven't specified the sizes of the columns so they have defaulted to the largest possible size.

You need to add something (like):

builder.setCRS(DefaultGeographicCRS.WGS84); 
builder.setName(plotIdentifier);

builder.add(sPolygon, Polygon.class);
//restrict length of string to 10 chars
builder.length(10);
builder.add(plotIdentifier, String.class);
builder.add(aRea,Float.class);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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