Hi group, last week I sent a message about how to create a new shapefile, I
do this with this code:
AttributeType[] types = new AttributeType[2];
GeometryFactory geomFac = new GeometryFactory();
//simpleline
Coordinate[] linestringCoordinates = new Coordinate[7];
linestringCoordinates[0] = new Coordinate(5.0d,5.0d);
linestringCoordinates[1] = new Coordinate(6.0d,5.0d);
linestringCoordinates[2] = new Coordinate(6.0d,6.0d);
linestringCoordinates[3] = new Coordinate(7.0d,6.0d);
linestringCoordinates[4] = new Coordinate(7.0d,7.0d);
linestringCoordinates[5] = new Coordinate(8.0d,7.0d);
linestringCoordinates[6] = new Coordinate(8.0d,8.0d);
LineString line = geomFac.createLineString(linestringCoordinates);
types[0] = AttributeTypeFactory.newAttributeType("centerline",
line.getClass());
types[1] = AttributeTypeFactory.newAttributeType("name", String.class);
FeatureType lineType = FeatureTypes.newFeatureType(types,
"linefeature");
Feature lineFeature = lineType.create(new Object[]{line, "centerline"});
//simple polygon
Coordinate[] polygonCoordinates = new Coordinate[10];
polygonCoordinates[0] = new Coordinate(7,7);
polygonCoordinates[1] = new Coordinate(6,9);
polygonCoordinates[2] = new Coordinate(6,11);
polygonCoordinates[3] = new Coordinate(7,12);
polygonCoordinates[4] = new Coordinate(9,11);
polygonCoordinates[5] = new Coordinate(11,12);
polygonCoordinates[6] = new Coordinate(13,11);
polygonCoordinates[7] = new Coordinate(13,9);
polygonCoordinates[8] = new Coordinate(11,7);
polygonCoordinates[9] = new Coordinate(7,7);
LinearRing ring = geomFac.createLinearRing(polygonCoordinates);
Polygon polygon = geomFac.createPolygon(ring,null);
types[0] = AttributeTypeFactory.newAttributeType("edge",
polygon.getClass());
types[1] = AttributeTypeFactory.newAttributeType("name", String.class);
FeatureType polygonType =
FeatureTypes.newFeatureType(types,"polygonfeature");
Feature polygonFeature = polygonType.create(new Object[]{polygon,
"edge"});
//simple point
Coordinate c = new Coordinate(14.0d,14.0d);
Point point = geomFac.createPoint(c);
types[0] = AttributeTypeFactory.newAttributeType("centre",
point.getClass());
types[1] = AttributeTypeFactory.newAttributeType("name", String.class);
FeatureType pointType =
FeatureTypes.newFeatureType(types,"pointfeature");
Feature pointFeature = pointType.create(new Object[]{point, "centre"});
//memory store
MemoryDataStore data = new MemoryDataStore();
data.addFeature(lineFeature);
data.addFeature(polygonFeature);
data.addFeature(pointFeature);
String folder= "D:\\Shapes_LFPR\\Lote 05";
URL shape = new File(folder+ "\\apr.dbf").toURL();
String newStoreURLString =
shape.toString().substring(0,shape.toString().lastIndexOf("/") + 1)
+ "test_" +
shape.toString().substring(shape.toString().lastIndexOf("/") + 1);
ShapefileDataStore newShapefileDataStore =
new ShapefileDataStore(new URL(newStoreURLString));
newShapefileDataStore.createSchema(data.getSchema("linefeature"));
FeatureSource newFeatureSource =
newShapefileDataStore.getFeatureSource("linefeature");
FeatureStore newFeatureStore = (FeatureStore)newFeatureSource;
Transaction t = newFeatureStore.getTransaction();
FeatureResults fsFilteredShape = newFeatureSource.getFeatures();
FeatureReader filteredReader = fsFilteredShape.reader();
newFeatureStore.addFeatures(filteredReader);
t.commit();
t.close();
System.out.println("Shapefile created");
This code generated 3 files : test_apr.dbf, test_apr.shp and test_apr.shx.
but, when I open this shapefile in a GIS program, like ArcGis, it throws the
following error: "One or more layers is missing spatial reference
information. Data from those layers cannot be projected."
How can I set the spatial reference on this test code?
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Geotools-gt2-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users