Hi Andreas,
I'm no expert, but have something that does work...
Looking at what I've done, you can only add objects of one of the simple
types (Point, LineString, Polygon) to a Shapefile... my code is below.
/**
* Create an output Shapefile
*
* @param filepath Path to file from Output/ plus filename (no extension)
* @param cls Class of objects to write (Point/LineString/Polygon)
* @param crs Coordinate Reference System
*/
public Shapefile(String filepath, final Class<?> cls,
CoordinateReferenceSystem crs) {
file = new File(filepath + ".shp");
// Build the schema with the correct CRS (WGS84 or NZGD2000)
SimpleFeatureTypeBuilder schemaBuilder = new SimpleFeatureTypeBuilder();
schemaBuilder.setCRS(crs);
// add attributes in order
schemaBuilder.setName(cls.getName());
schemaBuilder.add("geometry", cls);
schemaBuilder.length(25).add("name", String.class); // 25 chars width
for name field
schema = schemaBuilder.buildFeatureType();
builder = new SimpleFeatureBuilder(schema);
java.util.Map<String, Serializable> params = new HashMap<>(2);
try {
params.put("url", file.toURI().toURL());
params.put("create spatial index", Boolean.TRUE);
ShapefileDataStore dataStore = (ShapefileDataStore)
datastoreFactory.createNewDataStore(params);
dataStore.createSchema(schema);
transaction = new DefaultTransaction("create " + filepath);
writer = dataStore.getFeatureWriterAppend(transaction);
} catch (IOException ex) {
Util.crash(ex);
}
}
Adding features to the Shapefile is done as follows:
public void add(String name, Geometry geom) {
if (geom == null)
return;
if (geom instanceof Polygon) {
if (geom.getNumPoints() < 4)
return;
builder.add(geom);
if (name != null)
builder.add(name);
add(builder.buildFeature(null));
} else if (geom instanceof MultiPolygon) {
MultiPolygon mp = (MultiPolygon) geom;
for (int i = 0; i < mp.getNumGeometries(); i++) {
Polygon p = (Polygon) mp.getGeometryN(i);
builder.add(p);
if (name != null)
builder.add(name);
add(builder.buildFeature(null));
}
} else if (geom instanceof LineString) {
if (geom.getNumPoints() < 2)
return;
builder.add(geom);
if (name != null)
builder.add(name);
add(builder.buildFeature(null));
} else if (geom instanceof GeometryCollection) {
GeometryCollection gc = (GeometryCollection) geom;
for (int i = 0; i < gc.getNumGeometries(); i++) {
add(name, gc.getGeometryN(i));
}
} else
assert false: "Not a Polygon or LineString: " + name + " = " +
geom.getClass().getName();
}
Finally, you close the Shapefile
public void close() {
Log.i("Closing shapefile " + file.getPath() + " (" + numFeatures + ")");
try {
transaction.commit();
writer.close();
transaction.close();
} catch (IOException ex) {
Util.crash(ex);
}
}
Frank
On Sat, Jan 18, 2014 at 5:11 AM, Andrea Aime
<[email protected]>wrote:
> On Fri, Jan 17, 2014 at 12:47 PM, Andreas Bergmann
> <[email protected]>wrote:
>
>> Is the type MultiPolygon not supported?
>> Which type do I have to use in order to store a 'multi part polygon' in a
>> shapefile?
>>
>
> It is, but you have to declare the field as such, MultiPolygon.class
>
> Cheers
> Andrea
>
>
> --
> == Our support, Your Success! Visit http://opensdi.geo-solutions.it for
> more information ==
>
> Ing. Andrea Aime
> @geowolf
> Technical Lead
>
> GeoSolutions S.A.S.
> Via Poggio alle Viti 1187
> 55054 Massarosa (LU)
> Italy
> phone: +39 0584 962313
> fax: +39 0584 1660272
> mob: +39 339 8844549
>
> http://www.geo-solutions.it
> http://twitter.com/geosolutions_it
>
> -------------------------------------------------------
>
>
> ------------------------------------------------------------------------------
> CenturyLink Cloud: The Leader in Enterprise Cloud Services.
> Learn Why More Businesses Are Choosing CenturyLink Cloud For
> Critical Workloads, Development Environments & Everything In Between.
> Get a Quote or Start a Free Trial Today.
>
> http://pubads.g.doubleclick.net/gampad/clk?id=119420431&iu=/4140/ostg.clktrk
> _______________________________________________
> GeoTools-GT2-Users mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users
>
>
------------------------------------------------------------------------------
CenturyLink Cloud: The Leader in Enterprise Cloud Services.
Learn Why More Businesses Are Choosing CenturyLink Cloud For
Critical Workloads, Development Environments & Everything In Between.
Get a Quote or Start a Free Trial Today.
http://pubads.g.doubleclick.net/gampad/clk?id=119420431&iu=/4140/ostg.clktrk
_______________________________________________
GeoTools-GT2-Users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users