001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2002-2006, 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.caching;
017:
018: import com.vividsolutions.jts.geom.Coordinate;
019: import com.vividsolutions.jts.geom.CoordinateSequence;
020: import com.vividsolutions.jts.geom.Geometry;
021: import com.vividsolutions.jts.geom.GeometryFactory;
022: import com.vividsolutions.jts.geom.LineString;
023: import com.vividsolutions.jts.geom.impl.CoordinateArraySequence;
024:
025: import org.geotools.data.DefaultQuery;
026: import org.geotools.data.Query;
027:
028: import org.geotools.feature.AttributeType;
029: import org.geotools.feature.DefaultAttributeTypeFactory;
030: import org.geotools.feature.Feature;
031: import org.geotools.feature.FeatureType;
032: import org.geotools.feature.FeatureTypeBuilder;
033: import org.geotools.feature.IllegalAttributeException;
034: import org.geotools.feature.SchemaException;
035: import org.geotools.feature.type.GeometricAttributeType;
036:
037: import org.geotools.filter.FilterFactoryImpl;
038:
039: import org.geotools.referencing.crs.DefaultEngineeringCRS;
040:
041: import org.opengis.filter.Filter;
042: import org.opengis.filter.FilterFactory;
043:
044: import java.net.URI;
045:
046: import java.util.Random;
047:
048: public class Generator {
049: private static final FeatureType type = createFeatureType();
050: private static final GeometryFactory gfact = new GeometryFactory();
051: private static final Random rand = new Random();
052: private static final FilterFactory filterFactory = new FilterFactoryImpl();
053: private final double xrange;
054: private final double yrange;
055:
056: public Generator(double xrange, double yrange) {
057: this .xrange = xrange;
058: this .yrange = yrange;
059: }
060:
061: private static FeatureType createFeatureType() {
062: FeatureTypeBuilder builder = FeatureTypeBuilder
063: .newInstance("test");
064: GeometricAttributeType geom = new GeometricAttributeType(
065: "geom", Geometry.class, true, null,
066: DefaultEngineeringCRS.GENERIC_2D, Filter.INCLUDE);
067: AttributeType dummydata = DefaultAttributeTypeFactory
068: .newAttributeType("dummydata", String.class);
069: builder.addType(geom);
070: builder.addType(dummydata);
071: builder.setDefaultGeometry(geom);
072: builder.setNamespace(URI.create("testStore"));
073:
074: try {
075: FeatureType type = builder.getFeatureType();
076:
077: return type;
078: } catch (SchemaException e) {
079: throw (RuntimeException) new RuntimeException()
080: .initCause(e);
081: }
082: }
083:
084: private static LineString createRectangle(double x1, double y1,
085: double x2, double y2) {
086: double x_min = (x1 < x2) ? x1 : x2;
087: double y_min = (y1 < y2) ? y1 : y2;
088: double x_max = (x1 < x2) ? x2 : x1;
089: double y_max = (y1 < y2) ? y2 : y1;
090: Coordinate[] coords = new Coordinate[5];
091: coords[0] = new Coordinate(x_min, y_min);
092: coords[1] = new Coordinate(x_max, y_min);
093: coords[2] = new Coordinate(x_max, y_max);
094: coords[3] = new Coordinate(x_min, y_max);
095: coords[4] = coords[0];
096:
097: CoordinateSequence cs = new CoordinateArraySequence(coords);
098:
099: return new LineString(cs, gfact);
100: }
101:
102: public Feature createFeature(int i) {
103: Geometry g = createRectangle(xrange * rand.nextDouble(), yrange
104: * rand.nextDouble(), xrange * rand.nextDouble(), yrange
105: * rand.nextDouble());
106: String dummydata = "Id: " + i;
107: Feature f = null;
108:
109: try {
110: f = type.create(new Object[] { g, dummydata });
111:
112: return f;
113: } catch (IllegalAttributeException e) {
114: throw (RuntimeException) new RuntimeException()
115: .initCause(e);
116: }
117: }
118:
119: public static Coordinate pickRandomPoint(Coordinate center,
120: double xrange, double yrange) {
121: double x = (center.x - (xrange / 2))
122: + (xrange * rand.nextDouble());
123: double y = (center.y - (yrange / 2))
124: + (yrange * rand.nextDouble());
125:
126: return new Coordinate(x, y);
127: }
128:
129: public static Query createBboxQuery(Coordinate center,
130: double xrange, double yrange) {
131: double x_min = center.x - (xrange / 2);
132: double x_max = center.x + (xrange / 2);
133: double y_min = center.y - (yrange / 2);
134: double y_max = center.y + (yrange / 2);
135: Filter bb = filterFactory.bbox(type.getDefaultGeometry()
136: .getName(), x_min, y_min, x_max, y_max, type
137: .getDefaultGeometry().getCoordinateSystem().toString());
138:
139: return new DefaultQuery(type.getTypeName(), bb);
140: }
141:
142: public FeatureType getFeatureType() {
143: return type;
144: }
145:
146: public static void main(String[] args) {
147: if (args.length < 1) {
148: System.err.println("Usage : Generator number_of_data");
149: System.exit(0);
150: }
151:
152: int numberOfObjects = Integer.parseInt(args[0]);
153: Generator gen = new Generator(1000, 1000);
154:
155: for (int i = 0; i < numberOfObjects; i++) {
156: Feature f = gen.createFeature(i);
157: System.out.println(f);
158:
159: Coordinate c = pickRandomPoint(new Coordinate(500, 500),
160: 900, 900);
161: Query q = createBboxQuery(c, 100, 100);
162: System.out.println(q);
163: }
164: }
165: }
|