001: /*
002: * Geotools2 - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2002, Geotools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: */
016: package org.geotools.data.shapefile.shp;
017:
018: import java.awt.Dimension;
019: import java.io.File;
020: import java.io.IOException;
021: import java.net.URL;
022: import java.util.NoSuchElementException;
023:
024: import org.geotools.data.DataStore;
025: import org.geotools.data.DataUtilities;
026: import org.geotools.data.FeatureReader;
027: import org.geotools.data.FeatureStore;
028: import org.geotools.data.shapefile.indexed.IndexedShapefileDataStoreFactory;
029: import org.geotools.feature.AttributeTypeFactory;
030: import org.geotools.feature.Feature;
031: import org.geotools.feature.FeatureCollection;
032: import org.geotools.feature.FeatureType;
033: import org.geotools.feature.FeatureTypeBuilder;
034: import org.geotools.feature.GeometryAttributeType;
035: import org.geotools.feature.IllegalAttributeException;
036:
037: import com.vividsolutions.jts.geom.Coordinate;
038: import com.vividsolutions.jts.geom.GeometryFactory;
039: import com.vividsolutions.jts.geom.LineString;
040:
041: /**
042: *
043: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/extension/shapefile-renderer/src/test/java/org/geotools/data/shapefile/shp/createTestData.java $
044: */
045: public class createTestData {
046: public static URL createLineData(final Dimension d)
047: throws Exception {
048: File file = new File("test_lines.shp");
049:
050: if (file.exists()) {
051: file.delete();
052: }
053:
054: IndexedShapefileDataStoreFactory factory = new IndexedShapefileDataStoreFactory();
055: DataStore datastore = factory.createDataStore(file.toURL());
056: FeatureTypeBuilder builder = FeatureTypeBuilder
057: .newInstance("test_lines");
058: builder
059: .setDefaultGeometry((GeometryAttributeType) AttributeTypeFactory
060: .newAttributeType("geom", LineString.class));
061: builder.addType(AttributeTypeFactory.newAttributeType("x",
062: Integer.class));
063: builder.addType(AttributeTypeFactory.newAttributeType("y",
064: Integer.class));
065:
066: final FeatureType featureType = builder.getFeatureType();
067: datastore.createSchema(featureType);
068:
069: FeatureStore store = (FeatureStore) datastore
070: .getFeatureSource("test_lines");
071: FeatureCollection collection = DataUtilities
072: .collection(new FeatureReader() {
073: public FeatureType getFeatureType() {
074: return featureType;
075: }
076:
077: GeometryFactory factory = new GeometryFactory();
078: int x = 0;
079: int y = 0;
080:
081: public Feature next() throws IOException,
082: IllegalAttributeException,
083: NoSuchElementException {
084: LineString geom = factory
085: .createLineString(new Coordinate[] {
086: new Coordinate(x + 0.0, y + 0.0),
087: new Coordinate(x + .9, y + 0.9) });
088: Feature feature = featureType
089: .create(new Object[] { geom,
090: new Integer(x), new Integer(y) });
091:
092: if (x == (d.width - 1)) {
093: y++;
094: x = 0;
095: } else {
096: x++;
097: }
098:
099: return feature;
100: }
101:
102: public boolean hasNext() throws IOException {
103: return y < d.height;
104: }
105:
106: public void close() throws IOException {
107: // TODO Auto-generated method stub
108: }
109: });
110: store.addFeatures(collection);
111:
112: return file.toURL();
113: }
114:
115: /**
116: * DOCUMENT ME!
117: *
118: * @param args
119: *
120: * @throws Exception
121: */
122: public static void main(String[] args) throws Exception {
123: System.out.println(createLineData(new Dimension(512, 512)));
124: }
125: }
|