Hi John
> The question is due to my weak understanding of the API: Given a collection
> of LineString geometries (say, Array or LinkedList) of line strings, how can
> I turn that back into a shapefile?
it's quite straightforward but first you need to decide what
attributes you want to associate with each LineString. Your input data
are state and county polygons, so perhaps you would want to record the
identifying code / abbreviation / whatever of each state and county
that each line string was associated with ? And for LineString that
was originally part of a country polygon boundary but not a state
boundary, would you want to have some sort of N/A code for the state
attribute, or record the state that it falls in ?
Once you know all that (and after overcoming the simplification
challenges :) can create a feature type and line shapefile something
like this (edit for whatever attributes you are working with):
private void makeShapefile(List<LineString> lines,
CoordinateReferenceSystem crs) {
SimpleFeatureTypeBuilder typeBuilder = new SimpleFeatureTypeBuilder();
typeBuilder.setName("Political");
typeBuilder.setCRS(crs); // if you are using one
typeBuilder.add("the_geom", LineString.class);
typeBuilder.add("id", Integer.class);
typeBuilder.add("state1", String.class);
typeBuilder.add("state2", String.class);
typeBuilder.add("county1", String.class);
typeBuilder.add("county2", String.class);
final SimpleFeatureType TYPE = typeBuilder.buildFeatureType();
SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(TYPE);
SimpleFeatureCollection fc = FeatureCollections.newCollection();
for (LineString line : lines) {
int numVertices = line.getNumPoints();
LineSegment seg = new LineSegment(
line.getCoordinateN(0),
line.getCoordinateN(numVertices - 1));
LineData data = lineLookup.get(seg);
featureBuilder.addAll(new Object[] {
line,
data.id,
data.state1,
data.state2,
data.county1,
data.county2
});
fc.add(featureBuilder.buildFeature(String.valueOf(data.id)));
}
}
Obviously I just made up those attributes to convey the general idea.
The code assumes that you have a small class to store the attribute
data for each line; something like this...
class LineData {
int id;
String state1;
String state2;
String county1;
String county2;
}
And also a Map like this to associate the data object with each LineString.
Map<LineSegment, LineData> lineLookup;
It is using the LineString end-points, expressed as a LineSegment
object, as the key. I suggest you use a Map rather than the
Geometry.setUserData method to store the data in your LineStrings
directly. The reason is that if you put your lines through one of the
JTS XXXSimplifiers at any stage they strip the user data from the
Geometries.
Once you've got a SimpleFeatureCollection you can create a new
shapefile from it as in the cvs2shp example:
http://docs.geotools.org/stable/userguide/examples/csv2shp.html
Michael
------------------------------------------------------------------------------
Enable your software for Intel(R) Active Management Technology to meet the
growing manageability and security demands of your customers. Businesses
are taking advantage of Intel(R) vPro (TM) technology - will your software
be a part of the solution? Download the Intel(R) Manageability Checker
today! http://p.sf.net/sfu/intel-dev2devmar
_______________________________________________
Geotools-gt2-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users