Hi Cris,
Not got time to try it right now, but another quick thing to try is to rename
“Location” to “the_geom”. I’ll try to have a look in a bit…
Andy
From: Cris Lacumba [mailto:cris.lacu...@gmail.com]
Sent: 13 November 2014 13:09
To: Andy Turner
Cc: geotools-gt2-users@lists.sourceforge.net
Subject: Re: [Geotools-gt2-users] Fwd: [Create a SHP file with a polygon]
Hello Andy,
There is no problema with the file path. In fact that code creates a .shp,
.dbf, .fix, .prj and .shx files perfectly.
However, I can see the data (columns with Data1 and Data2 values) in the table,
but the geometry does not appear. That's the problem.
Can some of you try this code and try to open it in a GIS viewer? And see why
geometry does not appear?
Thanks again!
Cris
2014-11-13 12:48 GMT+01:00 Andy Turner
<a.g.d.tur...@leeds.ac.uk<mailto:a.g.d.tur...@leeds.ac.uk>>:
Hi Cris,
Not sure. The ruta filename looks a bit odd to me with backslashes. Do you get
the same problem if you replace the "\" with "/"?
Andy
________________________________________
From: Cris Lacumba [cris.lacu...@gmail.com<mailto:cris.lacu...@gmail.com>]
Sent: 13 November 2014 10:42
To:
geotools-gt2-users@lists.sourceforge.net<mailto:geotools-gt2-users@lists.sourceforge.net>
Subject: [Geotools-gt2-users] Fwd: [Create a SHP file with a polygon]
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