Hello to all,
I'm trying to create a SHP with a simple polygon but I'm not able to
achieve it.
Here is my sample code:
--
*package* GeotoolsPruebas;
*import* java.io.File;
*import* java.io.Serializable;
*import* java.util.HashMap;
*import* java.util.Map;
*import* com.vividsolutions.jts.geom.Coordinate;
*import* com.vividsolutions.jts.geom.GeometryFactory;
*import* com.vividsolutions.jts.geom.LinearRing;
*import* com.vividsolutions.jts.geom.Polygon;
*import* org.geotools.data.DefaultTransaction;
*import* org.geotools.data.Transaction;
*import* org.geotools.data.shapefile.ShapefileDataStore;
*import* org.geotools.data.shapefile.ShapefileDataStoreFactory;
*import* org.geotools.data.simple.SimpleFeatureSource;
*import* org.geotools.data.simple.SimpleFeatureStore;
*import* org.geotools.feature.DefaultFeatureCollection;
*import* org.geotools.feature.simple.SimpleFeatureBuilder;
*import* org.geotools.feature.simple.SimpleFeatureTypeBuilder;
*import* org.geotools.geometry.jts.JTSFactoryFinder;
*import* org.geotools.referencing.crs.DefaultGeographicCRS;
*import* org.opengis.feature.simple.SimpleFeature;
*import* org.opengis.feature.simple.SimpleFeatureType;
*public* *class* PruebaGeotools {
*public* PruebaGeotools() {
//Create *de* SHP Type
SimpleFeatureType TYPE = *createFeatureType*();
DefaultFeatureCollection collection = *new* DefaultFeatureCollection();
*try* {
GeometryFactory geometryFactory = JTSFactoryFinder.*getGeometryFactory*();
Coordinate[] coords =
*new* Coordinate[] {*new* Coordinate(4, 0), *new* Coordinate(2, 2),
*new* Coordinate(4, 4), *new* Coordinate(6, 2), *new* Coordinate(4, 0) };
LinearRing ring = geometryFactory.createLinearRing( coords );
LinearRing holes[] = *null*; // use LinearRing[] to represent holes
Polygon polygon = geometryFactory.createPolygon(ring, holes );
SimpleFeatureBuilder featureBuilder = *new* SimpleFeatureBuilder(TYPE);
featureBuilder.add(polygon);
featureBuilder.add("Data1");
featureBuilder.add("Data2");
SimpleFeature feature = featureBuilder.buildFeature("Element 1");
collection.add(feature);
//SAVE DATA TO A FILE
String ruta = "C:\\Data\\Test.shp";
File file = *new* File(ruta);
ShapefileDataStoreFactory dataStoreFactory = *new*
ShapefileDataStoreFactory();
Map<String, Serializable> params = *new* HashMap<String, Serializable>();
params.put("url", file.toURI().toURL());
params.put("create spatial index", Boolean.*TRUE*);
ShapefileDataStore newDataStore = (ShapefileDataStore) dataStoreFactory
.createNewDataStore(params);
newDataStore.createSchema(TYPE);
newDataStore.forceSchemaCRS(DefaultGeographicCRS.*WGS84*);
/*
* Write the features to the *shapefile*
*/
Transaction transaction = *new* DefaultTransaction("create");
String typeName = newDataStore.getTypeNames()[0];
SimpleFeatureSource featureSource = newDataStore.getFeatureSource(typeName);
*if* (featureSource *instanceof* SimpleFeatureStore) {
SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;
featureStore.setTransaction(transaction);
*try* {
//ADD ALL THE FEATURES
featureStore.addFeatures(collection);
transaction.commit();
} *catch* (Exception problem) {
problem.printStackTrace();
transaction.rollback();
} *finally* {
transaction.close();
}
System.*exit*(0); // success!
} *else* {
System.*out*.println(typeName + " does not support read/write access");
System.*exit*(1);
}
} *catch*(Exception e) {
e.printStackTrace();
}
}
*private* *static* SimpleFeatureType createFeatureType() {
SimpleFeatureTypeBuilder builder = *new* SimpleFeatureTypeBuilder();
builder.setName("Location");
builder.setCRS(DefaultGeographicCRS.*WGS84*); // <- Coordinate reference
system
// add attributes in order
builder.add("Location", Polygon.*class*);
builder.length(15).add("Name1", String.*class*); // <- 15 chars width for
name field
builder.length(15).add("Name2", String.*class*); // <- 15 chars width for
name field
// build the type
*final* SimpleFeatureType LOCATION = builder.buildFeatureType();
*return* LOCATION;
}
*public* *static* *void* main(String args[]) {
PruebaGeotools *objPruebaGeotools* = *new* PruebaGeotools();
}
}
--
What I'm doing wrong?
Thanks in advance!
Regards
Cris
------------------------------------------------------------------------------
Comprehensive Server Monitoring with Site24x7.
Monitor 10 servers for $9/Month.
Get alerted through email, SMS, voice calls or mobile push notifications.
Take corrective actions from your mobile device.
http://pubads.g.doubleclick.net/gampad/clk?id=154624111&iu=/4140/ostg.clktrk
_______________________________________________
GeoTools-GT2-Users mailing list
GeoTools-GT2-Users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users