http://git-wip-us.apache.org/repos/asf/zest-qi4j/blob/684bb9eb/libraries/spatial/src/main/java/org/qi4j/library/spatial/conversions/from/WKTFromConverter.java ---------------------------------------------------------------------- diff --git a/libraries/spatial/src/main/java/org/qi4j/library/spatial/conversions/from/WKTFromConverter.java b/libraries/spatial/src/main/java/org/qi4j/library/spatial/conversions/from/WKTFromConverter.java deleted file mode 100644 index 169c311..0000000 --- a/libraries/spatial/src/main/java/org/qi4j/library/spatial/conversions/from/WKTFromConverter.java +++ /dev/null @@ -1,226 +0,0 @@ -/* - * Copyright 2014 Jiri Jetmar. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or - * implied. - * - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.qi4j.library.spatial.conversions.from; - -import com.spatial4j.core.context.SpatialContext; -import com.spatial4j.core.context.jts.JtsSpatialContextFactory; -import com.spatial4j.core.io.jts.JtsWKTReaderShapeParser; -import com.spatial4j.core.io.jts.JtsWktShapeParser; -import com.spatial4j.core.shape.Circle; -import com.spatial4j.core.shape.Point; -import com.spatial4j.core.shape.Shape; -import com.spatial4j.core.shape.jts.JtsGeometry; -import com.spatial4j.core.shape.jts.JtsPoint; -import com.vividsolutions.jts.geom.Geometry; -import com.vividsolutions.jts.geom.LineString; -import com.vividsolutions.jts.geom.MultiPolygon; -import com.vividsolutions.jts.geom.Polygon; -import org.qi4j.api.geometry.TPoint; -import org.qi4j.api.geometry.TPolygon; -import org.qi4j.api.geometry.internal.TGeometry; -import org.qi4j.api.geometry.internal.TLinearRing; -import org.qi4j.api.structure.Module; -import org.qi4j.api.value.ValueBuilder; - -import java.text.ParseException; -import java.util.ArrayList; -import java.util.List; - -import static org.qi4j.api.geometry.TGeometryFactory.TPoint; - - -public class WKTFromConverter { - - - final SpatialContext ctx; - { - JtsSpatialContextFactory factory = new JtsSpatialContextFactory(); - factory.srid = 4326; - factory.datelineRule = JtsWktShapeParser.DatelineRule.ccwRect; - factory.wktShapeParserClass = JtsWKTReaderShapeParser.class; - ctx = factory.newSpatialContext(); - } - - - - private Module module; - - public WKTFromConverter(Module module) - { - this.module = module; - } - - public TGeometry convert(String wkt, String crs) throws ParseException - { - - Shape sNoDL = ctx.readShapeFromWkt(wkt); - - - if (!sNoDL.hasArea()) { - System.out.println("Its a JtsGeometry " + ((JtsPoint) sNoDL).getGeom().getGeometryType()); - return buildPoint(module,sNoDL); - } else { - System.out.println("Its a JtsGeometry " + ((JtsGeometry) sNoDL).getGeom().getGeometryType()); - - Geometry jtsGeometry = ((JtsGeometry) sNoDL).getGeom(); - - if (jtsGeometry instanceof Polygon) { - System.out.println("Polygon"); - return buildPolygon(module,sNoDL); - } - else if (jtsGeometry instanceof MultiPolygon) { - System.out.println("MultiPolygon"); - } - else if (jtsGeometry instanceof LineString) { - System.out.println("LineString"); - } - - - - - } - - - - if (sNoDL instanceof Point) - { - - // ctx.makeCircle(0,0,0).getBoundingBox(). - // System.out.println("Shape is point "); - - ValueBuilder<TPoint> builder = module.newValueBuilder(TPoint.class); - - builder.prototype().x(((Point) sNoDL).getX()).y(((Point) sNoDL).getY()); -/** - builder.prototype().of - ( - module.newValueBuilder(Coordinate.class).prototype().of(((Point) sNoDL).getX()), //x - module.newValueBuilder(Coordinate.class).prototype().of(((Point) sNoDL).getY()) //y - ); - */ - return builder.newInstance(); - - -// ValueBuilder<TGeomPoint> builder = module.newValueBuilder( TGeomPoint.class ); -// TGeomPoint proto = builder.prototype(); -// List<Double> coordinates = new ArrayList<Double>(); -//// -// Double lat = 3.138722; // 3.138722;// Double.parseDouble(query.nextToken()); -// Double lon = 101.386849; // Double.parseDouble(query.nextToken()); -//// -//// -// coordinates.add(3.138722); -// coordinates.add(101.386849); -// proto.coordinates().set(coordinates); -// -// return builder.newInstance(); - - } else - - if (sNoDL instanceof Circle) { - - } else - { - System.out.println("Its a shape"); - if (sNoDL.hasArea() ) System.out.println("Shape With area.."); - - if (sNoDL instanceof JtsGeometry) { - - System.out.println("Its a JtsGeometry " + ((JtsGeometry) sNoDL).getGeom().getGeometryType()); - // ((JtsGeometry) sNoDL).getGeom() - } - - } - - System.out.println("sNoDL " + sNoDL); - - return null; -} - - - private TPoint buildPoint(Module module, Shape sNoDL) { - ValueBuilder<TPoint> builder = module.newValueBuilder(TPoint.class); - - - builder.prototype().x(((Point) sNoDL).getX()).y(((Point) sNoDL).getY()); - /** - builder.prototype().of - ( - module.newValueBuilder(Coordinate.class).prototype().of(((Point) sNoDL).getX()), //x - module.newValueBuilder(Coordinate.class).prototype().of(((Point) sNoDL).getY()) //y - ); - */ - - return builder.newInstance(); - } - - private TPolygon buildPolygon(Module module, Shape sNoDL) { - - Geometry jtsGeometry = ((JtsGeometry) sNoDL).getGeom(); - Polygon jtsPolygon = (Polygon)jtsGeometry; - - // Polygon jtsPolygon = ((JtsGeometry) sNoDL).getGeom(); - - // Polygon jtsPolygon = (Polygon)sNoDL; - - System.out.println("Get Coordinates " + jtsPolygon.getExteriorRing().getCoordinates() ); - - com.vividsolutions.jts.geom.Coordinate[] coordinates = jtsPolygon.getExteriorRing().getCoordinates(); - - ValueBuilder<TPolygon> polygonBuilder = module.newValueBuilder(TPolygon.class); - ValueBuilder<TLinearRing> tLinearRingBuilder = module.newValueBuilder(TLinearRing.class); - - List<TPoint> points = new ArrayList<>(); - for (int i = 0; i < coordinates.length; i++) { -/** - TPoint point = module.newValueBuilder(TPoint.class).prototype().of - ( - module.newValueBuilder(Coordinate.class).prototype().of( coordinates[i].x), //x - module.newValueBuilder(Coordinate.class).prototype().of( coordinates[i].y) //y - );*/ - points.add - ( - TPoint(module) - - .x(coordinates[i].x) - .y(coordinates[i].y).geometry() - ); - } - tLinearRingBuilder.prototype().of(points); - - // tLinearRingBuilder.prototype().of(points); - // tLinearRingBuilder.prototype().type() - - // TLinearRing tLinearRing = tLinearRingBuilder.newInstance(); - // System.out.println("tLinearRing .." + tLinearRing); - - - ValueBuilder<TPolygon> builder = module.newValueBuilder(TPolygon.class); - - builder.prototype().of - ( - // tLinearRingBuilder.newInstance().of(points) - tLinearRingBuilder.newInstance() - ); - - return builder.newInstance(); - } - - -}
http://git-wip-us.apache.org/repos/asf/zest-qi4j/blob/684bb9eb/libraries/spatial/src/main/java/org/qi4j/library/spatial/conversions/to/GeoJsonToConverter.java ---------------------------------------------------------------------- diff --git a/libraries/spatial/src/main/java/org/qi4j/library/spatial/conversions/to/GeoJsonToConverter.java b/libraries/spatial/src/main/java/org/qi4j/library/spatial/conversions/to/GeoJsonToConverter.java deleted file mode 100644 index 595431a..0000000 --- a/libraries/spatial/src/main/java/org/qi4j/library/spatial/conversions/to/GeoJsonToConverter.java +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright 2014 Jiri Jetmar. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or - * implied. - * - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.qi4j.library.spatial.conversions.to; - -import org.geojson.GeoJsonObject; -import org.geojson.Point; -import org.qi4j.api.geometry.TPoint; -import org.qi4j.api.geometry.internal.TGeometry; -import org.qi4j.api.structure.Module; - - -public class GeoJsonToConverter { - - - private Module module; - - public GeoJsonToConverter(Module module) - { - this.module = module; - } - - public GeoJsonObject convert(TGeometry intemediate) - { - return transform(intemediate); - } - - private GeoJsonObject transform(TGeometry intemediate) - { - - switch(intemediate.getType()) - { - case POINT : return buildPoint((TPoint)intemediate); - case MULTIPOINT : return null; - case LINESTRING : return null; - case MULTILINESTRING : return null; - case POLYGON : return null; - case MULTIPOLYGON : return null; - case FEATURE : return null; - case FEATURECOLLECTION : return null; - default : throw new RuntimeException("Unknown TGeometry Type."); - } - - } - - private Point buildPoint(TPoint point) - { - return new Point(point.x(), point.y()); - } - -} http://git-wip-us.apache.org/repos/asf/zest-qi4j/blob/684bb9eb/libraries/spatial/src/main/java/org/qi4j/library/spatial/conversions/to/Spatial4JToConverter.java ---------------------------------------------------------------------- diff --git a/libraries/spatial/src/main/java/org/qi4j/library/spatial/conversions/to/Spatial4JToConverter.java b/libraries/spatial/src/main/java/org/qi4j/library/spatial/conversions/to/Spatial4JToConverter.java deleted file mode 100644 index e60f30c..0000000 --- a/libraries/spatial/src/main/java/org/qi4j/library/spatial/conversions/to/Spatial4JToConverter.java +++ /dev/null @@ -1,117 +0,0 @@ -/* - * Copyright 2014 Jiri Jetmar. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or - * implied. - * - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.qi4j.library.spatial.conversions.to; - -import com.spatial4j.core.context.SpatialContext; -import com.spatial4j.core.context.jts.JtsSpatialContext; -import com.spatial4j.core.context.jts.JtsSpatialContextFactory; -import com.spatial4j.core.io.jts.JtsWKTReaderShapeParser; -import com.spatial4j.core.io.jts.JtsWktShapeParser; -import com.spatial4j.core.shape.Circle; -import com.spatial4j.core.shape.Shape; -import com.vividsolutions.jts.geom.GeometryFactory; -import com.vividsolutions.jts.util.GeometricShapeFactory; -import org.geojson.GeoJsonObject; -import org.geojson.Point; -import org.qi4j.api.geometry.TPoint; -import org.qi4j.api.geometry.internal.TCircle; -import org.qi4j.api.geometry.internal.TGeometry; -import org.qi4j.api.structure.Module; - - -public class Spatial4JToConverter { - - // http://code.google.com/p/shape-metrics/source/browse/trunk/src/main/java/edu/psu/geovista/ian/utils/Circle.java?r=2 - - final SpatialContext ctx; - { - JtsSpatialContextFactory factory = new JtsSpatialContextFactory(); - factory.srid = 4326; - factory.datelineRule = JtsWktShapeParser.DatelineRule.ccwRect; - factory.wktShapeParserClass = JtsWKTReaderShapeParser.class; - ctx = factory.newSpatialContext(); - } - - public static final double DATELINE = 180; - public static final JtsSpatialContext SPATIAL_CONTEXT = JtsSpatialContext.GEO; - public static final GeometryFactory FACTORY = SPATIAL_CONTEXT.getGeometryFactory(); - public static final GeometricShapeFactory SHAPE_FACTORY = new GeometricShapeFactory(); - - protected final boolean multiPolygonMayOverlap = false; - protected final boolean autoValidateJtsGeometry = true; - protected final boolean autoIndexJtsGeometry = true; - - protected final boolean wrapdateline = SPATIAL_CONTEXT.isGeo(); - - - private Module module; - - public Spatial4JToConverter(Module module) - { - this.module = module; - } - - public GeoJsonObject convert(TGeometry intemediate) - { - // return transform(intemediate); - return null; - } - - private Shape transform(TGeometry intermediate) - { - - switch(intermediate.getType()) - { - case POINT : return null; // return newPoint((TPoint) intemediate); - case MULTIPOINT : return null; - case LINESTRING : return null; - case MULTILINESTRING : return null; - case POLYGON : return null; - case MULTIPOLYGON : return null; - case FEATURE : return null; - case FEATURECOLLECTION : return null; - } - - if (intermediate instanceof TCircle) - { - return newCircle((TCircle)intermediate); - } - else - return null; - - - } - - private Point newPoint(TPoint point) - { - // SPATIAL_CONTEXT.makeCircle(0,0,0). - - // SHAPE_FACTORY.set - - System.out.println("point.x() " + point.x()); - System.out.println("point.y() " + point.y()); - return new Point(point.x(), point.y()); - } - - private Circle newCircle(TCircle circle) - { - return ctx.makeCircle(circle.getCentre().x(), circle.getCentre().y(), circle.radius().get()); - } - -} http://git-wip-us.apache.org/repos/asf/zest-qi4j/blob/684bb9eb/libraries/spatial/src/main/java/org/qi4j/library/spatial/conversions/to/TGeometryToConverter.java ---------------------------------------------------------------------- diff --git a/libraries/spatial/src/main/java/org/qi4j/library/spatial/conversions/to/TGeometryToConverter.java b/libraries/spatial/src/main/java/org/qi4j/library/spatial/conversions/to/TGeometryToConverter.java deleted file mode 100644 index 01eaeed..0000000 --- a/libraries/spatial/src/main/java/org/qi4j/library/spatial/conversions/to/TGeometryToConverter.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright 2014 Jiri Jetmar. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or - * implied. - * - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.qi4j.library.spatial.conversions.to; - -import org.qi4j.api.geometry.internal.TGeometry; -import org.qi4j.api.structure.Module; - -/** - * Created by jj on 04.12.14. - */ -public class TGeometryToConverter { - - private Module module; - - public TGeometryToConverter(Module module) - { - this.module = module; - } - - public TGeometry convert (TGeometry tGeometry, String CRS) - { - return tGeometry; - } - - public TGeometry convert(TGeometry tGeometry) - { - return convert(tGeometry, tGeometry.getCRS()); - } - -} http://git-wip-us.apache.org/repos/asf/zest-qi4j/blob/684bb9eb/libraries/spatial/src/main/java/org/qi4j/library/spatial/formats/conversions/TConversions.java ---------------------------------------------------------------------- diff --git a/libraries/spatial/src/main/java/org/qi4j/library/spatial/formats/conversions/TConversions.java b/libraries/spatial/src/main/java/org/qi4j/library/spatial/formats/conversions/TConversions.java new file mode 100644 index 0000000..e8881ae --- /dev/null +++ b/libraries/spatial/src/main/java/org/qi4j/library/spatial/formats/conversions/TConversions.java @@ -0,0 +1,31 @@ +/* + * Copyright 2014 Jiri Jetmar. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + * implied. + * + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.qi4j.library.spatial.formats.conversions; + +import org.qi4j.api.structure.Module; +import org.qi4j.library.spatial.formats.conversions.from.FromHelper; + +public class TConversions +{ + + public static FromHelper Convert(Module module) + { + return new FromHelper(module); + } +} http://git-wip-us.apache.org/repos/asf/zest-qi4j/blob/684bb9eb/libraries/spatial/src/main/java/org/qi4j/library/spatial/formats/conversions/from/TGeometryFromConverter.java ---------------------------------------------------------------------- diff --git a/libraries/spatial/src/main/java/org/qi4j/library/spatial/formats/conversions/from/TGeometryFromConverter.java b/libraries/spatial/src/main/java/org/qi4j/library/spatial/formats/conversions/from/TGeometryFromConverter.java new file mode 100644 index 0000000..2c05866 --- /dev/null +++ b/libraries/spatial/src/main/java/org/qi4j/library/spatial/formats/conversions/from/TGeometryFromConverter.java @@ -0,0 +1,39 @@ +/* + * Copyright 2014 Jiri Jetmar. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + * implied. + * + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.qi4j.library.spatial.formats.conversions.from; + +import org.qi4j.api.geometry.internal.TGeometry; +import org.qi4j.api.structure.Module; + + +public class TGeometryFromConverter +{ + + private Module module; + + public TGeometryFromConverter(Module module) + { + this.module = module; + } + + public TGeometry convert(TGeometry tGeometry) + { + return tGeometry; + } +} http://git-wip-us.apache.org/repos/asf/zest-qi4j/blob/684bb9eb/libraries/spatial/src/main/java/org/qi4j/library/spatial/formats/conversions/from/WKTFromConverter.java ---------------------------------------------------------------------- diff --git a/libraries/spatial/src/main/java/org/qi4j/library/spatial/formats/conversions/from/WKTFromConverter.java b/libraries/spatial/src/main/java/org/qi4j/library/spatial/formats/conversions/from/WKTFromConverter.java new file mode 100644 index 0000000..5719376 --- /dev/null +++ b/libraries/spatial/src/main/java/org/qi4j/library/spatial/formats/conversions/from/WKTFromConverter.java @@ -0,0 +1,164 @@ +/* + * Copyright 2014 Jiri Jetmar. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + * implied. + * + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.qi4j.library.spatial.formats.conversions.from; + +import com.spatial4j.core.context.SpatialContext; +import com.spatial4j.core.context.jts.JtsSpatialContextFactory; +import com.spatial4j.core.io.jts.JtsWKTReaderShapeParser; +import com.spatial4j.core.io.jts.JtsWktShapeParser; +import com.spatial4j.core.shape.Circle; +import com.spatial4j.core.shape.Point; +import com.spatial4j.core.shape.Shape; +import com.spatial4j.core.shape.jts.JtsGeometry; +import com.vividsolutions.jts.geom.Geometry; +import com.vividsolutions.jts.geom.LineString; +import com.vividsolutions.jts.geom.MultiPolygon; +import com.vividsolutions.jts.geom.Polygon; +import org.qi4j.api.geometry.TPoint; +import org.qi4j.api.geometry.TPolygon; +import org.qi4j.api.geometry.internal.TGeometry; +import org.qi4j.api.geometry.internal.TLinearRing; +import org.qi4j.api.structure.Module; +import org.qi4j.api.value.ValueBuilder; + +import java.text.ParseException; +import java.util.ArrayList; +import java.util.List; + +import static org.qi4j.api.geometry.TGeometryFactory.TPoint; + + +public class WKTFromConverter +{ + + + final SpatialContext ctx; + + { + JtsSpatialContextFactory factory = new JtsSpatialContextFactory(); + factory.srid = 4326; + factory.datelineRule = JtsWktShapeParser.DatelineRule.ccwRect; + factory.wktShapeParserClass = JtsWKTReaderShapeParser.class; + ctx = factory.newSpatialContext(); + } + + + private Module module; + + public WKTFromConverter(Module module) + { + this.module = module; + } + + public TGeometry convert(String wkt, String crs) throws ParseException + { + + Shape sNoDL = ctx.readShapeFromWkt(wkt); + + + if (!sNoDL.hasArea()) + { + return buildPoint(module, sNoDL); + } else + { + + Geometry jtsGeometry = ((JtsGeometry) sNoDL).getGeom(); + + if (jtsGeometry instanceof Polygon) + { + return buildPolygon(module, sNoDL); + } else if (jtsGeometry instanceof MultiPolygon) + { + } else if (jtsGeometry instanceof LineString) + { + } + } + + + if (sNoDL instanceof Point) + { + + + ValueBuilder<TPoint> builder = module.newValueBuilder(TPoint.class); + builder.prototype().x(((Point) sNoDL).getX()).y(((Point) sNoDL).getY()); + return builder.newInstance(); + } else if (sNoDL instanceof Circle) + { + + } else + { + if (sNoDL.hasArea()) System.out.println("Shape With area.."); + + if (sNoDL instanceof JtsGeometry) + { + + } + + } + + + return null; + } + + + private TPoint buildPoint(Module module, Shape sNoDL) + { + ValueBuilder<TPoint> builder = module.newValueBuilder(TPoint.class); + builder.prototype().x(((Point) sNoDL).getX()).y(((Point) sNoDL).getY()); + return builder.newInstance(); + } + + private TPolygon buildPolygon(Module module, Shape sNoDL) + { + + Geometry jtsGeometry = ((JtsGeometry) sNoDL).getGeom(); + Polygon jtsPolygon = (Polygon) jtsGeometry; + + com.vividsolutions.jts.geom.Coordinate[] coordinates = jtsPolygon.getExteriorRing().getCoordinates(); + + ValueBuilder<TPolygon> polygonBuilder = module.newValueBuilder(TPolygon.class); + ValueBuilder<TLinearRing> tLinearRingBuilder = module.newValueBuilder(TLinearRing.class); + + List<TPoint> points = new ArrayList<>(); + for (int i = 0; i < coordinates.length; i++) + { + + points.add + ( + TPoint(module) + + .x(coordinates[i].x) + .y(coordinates[i].y).geometry() + ); + } + tLinearRingBuilder.prototype().of(points); + + + ValueBuilder<TPolygon> builder = module.newValueBuilder(TPolygon.class); + + builder.prototype().of + ( + tLinearRingBuilder.newInstance() + ); + + return builder.newInstance(); + } + + +} http://git-wip-us.apache.org/repos/asf/zest-qi4j/blob/684bb9eb/libraries/spatial/src/main/java/org/qi4j/library/spatial/formats/conversions/to/Spatial4JToConverter.java ---------------------------------------------------------------------- diff --git a/libraries/spatial/src/main/java/org/qi4j/library/spatial/formats/conversions/to/Spatial4JToConverter.java b/libraries/spatial/src/main/java/org/qi4j/library/spatial/formats/conversions/to/Spatial4JToConverter.java new file mode 100644 index 0000000..27e2237 --- /dev/null +++ b/libraries/spatial/src/main/java/org/qi4j/library/spatial/formats/conversions/to/Spatial4JToConverter.java @@ -0,0 +1,114 @@ +/* + * Copyright 2014 Jiri Jetmar. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + * implied. + * + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.qi4j.library.spatial.formats.conversions.to; + +import com.spatial4j.core.context.SpatialContext; +import com.spatial4j.core.context.jts.JtsSpatialContext; +import com.spatial4j.core.context.jts.JtsSpatialContextFactory; +import com.spatial4j.core.io.jts.JtsWKTReaderShapeParser; +import com.spatial4j.core.io.jts.JtsWktShapeParser; +import com.spatial4j.core.shape.Circle; +import com.spatial4j.core.shape.Shape; +import com.vividsolutions.jts.geom.GeometryFactory; +import com.vividsolutions.jts.util.GeometricShapeFactory; +import org.geojson.GeoJsonObject; +import org.geojson.Point; +import org.qi4j.api.geometry.TPoint; +import org.qi4j.api.geometry.internal.TCircle; +import org.qi4j.api.geometry.internal.TGeometry; +import org.qi4j.api.structure.Module; + + +public class Spatial4JToConverter +{ + + + public static final double DATELINE = 180; + public static final JtsSpatialContext SPATIAL_CONTEXT = JtsSpatialContext.GEO; + public static final GeometryFactory FACTORY = SPATIAL_CONTEXT.getGeometryFactory(); + public static final GeometricShapeFactory SHAPE_FACTORY = new GeometricShapeFactory(); + protected final boolean multiPolygonMayOverlap = false; + protected final boolean autoValidateJtsGeometry = true; + protected final boolean autoIndexJtsGeometry = true; + protected final boolean wrapdateline = SPATIAL_CONTEXT.isGeo(); + final SpatialContext ctx; + { + JtsSpatialContextFactory factory = new JtsSpatialContextFactory(); + factory.srid = 4326; + factory.datelineRule = JtsWktShapeParser.DatelineRule.ccwRect; + factory.wktShapeParserClass = JtsWKTReaderShapeParser.class; + ctx = factory.newSpatialContext(); + } + private Module module; + + public Spatial4JToConverter(Module module) + { + this.module = module; + } + + public GeoJsonObject convert(TGeometry intemediate) + { + // return transform(intemediate); + return null; + } + + private Shape transform(TGeometry intermediate) + { + + switch (intermediate.getType()) + { + case POINT: + return null; // return newPoint((TPoint) intemediate); + case MULTIPOINT: + return null; + case LINESTRING: + return null; + case MULTILINESTRING: + return null; + case POLYGON: + return null; + case MULTIPOLYGON: + return null; + case FEATURE: + return null; + case FEATURECOLLECTION: + return null; + } + + if (intermediate instanceof TCircle) + { + return newCircle((TCircle) intermediate); + } else + return null; + + + } + + private Point newPoint(TPoint point) + { + + return new Point(point.x(), point.y()); + } + + private Circle newCircle(TCircle circle) + { + return ctx.makeCircle(circle.getCentre().x(), circle.getCentre().y(), circle.radius().get()); + } + +} http://git-wip-us.apache.org/repos/asf/zest-qi4j/blob/684bb9eb/libraries/spatial/src/main/java/org/qi4j/library/spatial/formats/conversions/to/TGeometryToConverter.java ---------------------------------------------------------------------- diff --git a/libraries/spatial/src/main/java/org/qi4j/library/spatial/formats/conversions/to/TGeometryToConverter.java b/libraries/spatial/src/main/java/org/qi4j/library/spatial/formats/conversions/to/TGeometryToConverter.java new file mode 100644 index 0000000..c80c06c --- /dev/null +++ b/libraries/spatial/src/main/java/org/qi4j/library/spatial/formats/conversions/to/TGeometryToConverter.java @@ -0,0 +1,47 @@ +/* + * Copyright 2014 Jiri Jetmar. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + * implied. + * + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.qi4j.library.spatial.formats.conversions.to; + +import org.qi4j.api.geometry.internal.TGeometry; +import org.qi4j.api.structure.Module; + +/** + * Created by jj on 04.12.14. + */ +public class TGeometryToConverter +{ + + private Module module; + + public TGeometryToConverter(Module module) + { + this.module = module; + } + + public TGeometry convert(TGeometry tGeometry, String CRS) + { + return tGeometry; + } + + public TGeometry convert(TGeometry tGeometry) + { + return convert(tGeometry, tGeometry.getCRS()); + } + +} http://git-wip-us.apache.org/repos/asf/zest-qi4j/blob/684bb9eb/libraries/spatial/src/main/java/org/qi4j/library/spatial/formats/conversions/to/ToHelper.java ---------------------------------------------------------------------- diff --git a/libraries/spatial/src/main/java/org/qi4j/library/spatial/formats/conversions/to/ToHelper.java b/libraries/spatial/src/main/java/org/qi4j/library/spatial/formats/conversions/to/ToHelper.java new file mode 100644 index 0000000..4103075 --- /dev/null +++ b/libraries/spatial/src/main/java/org/qi4j/library/spatial/formats/conversions/to/ToHelper.java @@ -0,0 +1,63 @@ +/* + * Copyright 2014 Jiri Jetmar. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + * implied. + * + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.qi4j.library.spatial.formats.conversions.to; + +import org.geojson.GeoJsonObject; +import org.qi4j.api.geometry.internal.TGeometry; +import org.qi4j.api.structure.Module; + +import static org.qi4j.library.spatial.projections.transformations.TTransformations.Transform; + +/** + * Created by jj on 04.12.14. + */ +public class ToHelper +{ + + private Module module; + private TGeometry intermediate; + + public ToHelper(Module module, TGeometry intermediate) + { + this.module = module; + this.intermediate = intermediate; + } + + private ToHelper() + { + } + + public TGeometry toTGeometry() + { + return new TGeometryToConverter(module).convert(intermediate); + } + + public TGeometry toTGeometry(String CRS) throws Exception + { + if (!intermediate.getCRS().equalsIgnoreCase(CRS)) + Transform(module).from(intermediate).to(CRS); + + return new TGeometryToConverter(module).convert(intermediate, CRS); + } + + public GeoJsonObject toGeoJson() + { + return new GeoJsonToConverter(module).convert(intermediate); + } +} http://git-wip-us.apache.org/repos/asf/zest-qi4j/blob/684bb9eb/libraries/spatial/src/main/java/org/qi4j/library/spatial/projection/transformations/fromto/ToHelper.java ---------------------------------------------------------------------- diff --git a/libraries/spatial/src/main/java/org/qi4j/library/spatial/projection/transformations/fromto/ToHelper.java b/libraries/spatial/src/main/java/org/qi4j/library/spatial/projection/transformations/fromto/ToHelper.java deleted file mode 100644 index 752dffd..0000000 --- a/libraries/spatial/src/main/java/org/qi4j/library/spatial/projection/transformations/fromto/ToHelper.java +++ /dev/null @@ -1,150 +0,0 @@ -/* - * Copyright 2014 Jiri Jetmar. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or - * implied. - * - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.qi4j.library.spatial.projection.transformations.fromto; - -import org.cts.IllegalCoordinateException; -import org.cts.crs.GeodeticCRS; -import org.cts.op.CoordinateOperation; -import org.cts.op.CoordinateOperationFactory; -import org.qi4j.api.geometry.*; -import org.qi4j.api.geometry.internal.Coordinate; -import org.qi4j.api.geometry.internal.TGeometry; -import org.qi4j.api.structure.Module; -import org.qi4j.library.spatial.projection.ProjectionsRegistry; - -import java.util.List; - - -public class ToHelper -{ - - private Module module; - private TGeometry intermediate; - private double maxPrecisionMeanConversionError = Double.MAX_VALUE; - - public ToHelper(Module module, TGeometry intermediate) - { - this.module = module; - this.intermediate = intermediate; - } - - private ToHelper() {} - - - public void to(String CRS, double maxPrecisionMeanConversionError) throws RuntimeException - { - this.maxPrecisionMeanConversionError = maxPrecisionMeanConversionError; - to(CRS); - } - - public void to(String CRS) throws RuntimeException - { - try - { - GeodeticCRS sourceCRS = (GeodeticCRS) new ProjectionsRegistry().getCRS(intermediate.getCRS()); - GeodeticCRS targetCRS = (GeodeticCRS) new ProjectionsRegistry().getCRS(CRS); - - - if (sourceCRS.equals(targetCRS)) { - return; - } - switch (intermediate.getType()) { - case POINT: - transform(sourceCRS, targetCRS, new Coordinate[]{((TPoint) intermediate).getCoordinate()}); - break; - case MULTIPOINT: - transform(sourceCRS, targetCRS, ((TMultiPoint) intermediate).getCoordinates()); - break; - case LINESTRING: - transform(sourceCRS, targetCRS, ((TLineString) intermediate).getCoordinates()); - break; - case MULTILINESTRING: - transform(sourceCRS, targetCRS, ((TMultiLineString) intermediate).getCoordinates()); - break; - case POLYGON: - transform(sourceCRS, targetCRS, ((TPolygon) intermediate).getCoordinates()); - break; - case MULTIPOLYGON: - transform(sourceCRS, targetCRS, ((TMultiPolygon) intermediate).getCoordinates()); - break; - case FEATURE: - transform(sourceCRS, targetCRS, ((TFeature) intermediate).getCoordinates()); - break; - case FEATURECOLLECTION: - transform(sourceCRS, targetCRS, ((TFeatureCollection) intermediate).getCoordinates()); - break; - } - - // JJ TODO - Set nested TGeometries CRSs as well - intermediate.setCRS(targetCRS.getCode()); - } catch(Exception _ex) - { - throw new RuntimeException(_ex); - } - } - - - - private void transformTPoint(GeodeticCRS sourceCRS, GeodeticCRS targetCRS, TPoint tPoint ) throws Exception - { - transform(sourceCRS, targetCRS, new Coordinate[] { tPoint.getCoordinate() }); - tPoint.setCRS(targetCRS.getCode()); - } - - private void transformTMultiPoint(GeodeticCRS sourceCRS, GeodeticCRS targetCRS, TMultiPoint tMultiPoint ) throws Exception - { - transform(sourceCRS, targetCRS, tMultiPoint.getCoordinates()); - tMultiPoint.setCRS(targetCRS.getCode()); - // tMultiPoint. - } - - private void transformTLineString(GeodeticCRS sourceCRS, GeodeticCRS targetCRS, TLineString tLineString ) throws Exception - { - - transform(sourceCRS, targetCRS, tLineString.getCoordinates()); - tLineString.setCRS(targetCRS.getCode()); - // tMultiPoint. - } - - - private void transform(GeodeticCRS sourceCRS, GeodeticCRS targetCRS, Coordinate... coordinates) throws IllegalCoordinateException - { - List<CoordinateOperation> ops; - ops = CoordinateOperationFactory.createCoordinateOperations(sourceCRS, targetCRS); - - if (!ops.isEmpty()) { - if (true) { - System.out.println("Number of Operations " + ops.size()); - System.out.println("Precision " + ops.get(0).getPrecision() + " m."); - System.out.println(ops.get(0)); - } - - if (maxPrecisionMeanConversionError < Double.MAX_VALUE && maxPrecisionMeanConversionError < ops.get(0).getPrecision()) - throw new RuntimeException("Transformation from " + sourceCRS.getCode() + " to " + targetCRS.getCode() + - " can not be done with the requested precision of " + maxPrecisionMeanConversionError + " meters." + - " Current precision mean conversion error is " + ops.get(0).getPrecision() + " meters."); - - for (Coordinate coordinate : coordinates) - { - double[] c = ops.get(0).transform(new double[]{coordinate.y(), coordinate.x() /** z */} ); - coordinate.y(c[0]).x(c[1]); - } - } - } -} http://git-wip-us.apache.org/repos/asf/zest-qi4j/blob/684bb9eb/libraries/spatial/src/main/java/org/qi4j/library/spatial/projections/transformations/TTransformations.java ---------------------------------------------------------------------- diff --git a/libraries/spatial/src/main/java/org/qi4j/library/spatial/projections/transformations/TTransformations.java b/libraries/spatial/src/main/java/org/qi4j/library/spatial/projections/transformations/TTransformations.java new file mode 100644 index 0000000..657c377 --- /dev/null +++ b/libraries/spatial/src/main/java/org/qi4j/library/spatial/projections/transformations/TTransformations.java @@ -0,0 +1,32 @@ +/* + * Copyright 2014 Jiri Jetmar. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + * implied. + * + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.qi4j.library.spatial.projections.transformations; + +import org.qi4j.api.structure.Module; +import org.qi4j.library.spatial.projections.transformations.fromto.FromHelper; + + +public class TTransformations +{ + + public static FromHelper Transform(Module module) + { + return new FromHelper(module); + } +} http://git-wip-us.apache.org/repos/asf/zest-qi4j/blob/684bb9eb/libraries/spatial/src/test/java/org/qi4j/library/spatial/conversions/ConvertFromGeoJsonToTGeometryTest.java ---------------------------------------------------------------------- diff --git a/libraries/spatial/src/test/java/org/qi4j/library/spatial/conversions/ConvertFromGeoJsonToTGeometryTest.java b/libraries/spatial/src/test/java/org/qi4j/library/spatial/conversions/ConvertFromGeoJsonToTGeometryTest.java deleted file mode 100644 index 377eebc..0000000 --- a/libraries/spatial/src/test/java/org/qi4j/library/spatial/conversions/ConvertFromGeoJsonToTGeometryTest.java +++ /dev/null @@ -1,179 +0,0 @@ -/* - * Copyright 2014 Jiri Jetmar. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or - * implied. - * - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.qi4j.library.spatial.conversions; - -import com.fasterxml.jackson.databind.ObjectMapper; -import org.geojson.*; -import org.junit.Ignore; -import org.junit.Test; -import org.qi4j.api.geometry.*; -import org.qi4j.bootstrap.AssemblyException; -import org.qi4j.bootstrap.ModuleAssembly; -import org.qi4j.library.spatial.assembly.TGeometryAssembler; -import org.qi4j.test.AbstractQi4jTest; - -import java.util.Arrays; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; -import static org.qi4j.api.geometry.TGeometryFactory.*; - - -public class ConvertFromGeoJsonToTGeometryTest extends AbstractQi4jTest { - - private final String CRS_EPSG_4326_ = "EPSG:4326"; - private final String CRS_EPSG_27572 = "EPSG:27572"; - private ObjectMapper geoJsonMapper = new ObjectMapper(); - - @Override - public void assemble(ModuleAssembly module) - throws AssemblyException { - new TGeometryAssembler().assemble(module); - } - - @Ignore("Benchmarking is not in scope for this test.") - @Test - public void whenConvertFromTGeometryToTGeometry() throws Exception { - TPoint tPoint1 = TPoint(module).x(11.57958981111).y(48.13905780941111).geometry(); - for (int i = 0; i < 1000000; i++) { - TPoint tPoint2 = (TPoint) TConversions.Convert(module).from(tPoint1).toTGeometry(CRS_EPSG_27572); - TPoint tPoint3 = (TPoint) TConversions.Convert(module).from(tPoint1).toTGeometry(CRS_EPSG_4326_); - - } - } - - @Test - public void whenConvertPointFromGeoJsonToTGeometry() { - TPoint tPoint = TPoint(module).y(100).x(0).geometry(); - Point gPoint = new Point(100, 0); - TPoint convTPoint = (TPoint) TConversions.Convert(module).from(gPoint).toTGeometry(); - assertTrue(tPoint.compareTo(convTPoint) == 0); - } - - @Test - public void whenConvertMultiPointFromGeoJsonToTGeometry() { - TMultiPoint tMultiPoint = TMultiPoint(module).of - ( - TPoint(module).y(1).x(1).geometry(), - TPoint(module).y(2).x(2).geometry() - ).geometry(); - MultiPoint multiPoint = new MultiPoint(new LngLatAlt(1, 1), new LngLatAlt(2, 2)); - TMultiPoint convTMultiPoint = (TMultiPoint) TConversions.Convert(module).from(multiPoint).toTGeometry(); - assertTrue(((TPoint) tMultiPoint.getGeometryN(0)).compareTo(convTMultiPoint.getGeometryN(0)) == 0); - } - - - @Test - public void whenConvertLineStringFromGeoJsonToTGeometry() throws Exception { - LineString lineString = geoJsonMapper.readValue("{\"type\":\"LineString\",\"coordinates\":[[100.0,0.0],[101.0,1.0]]}", - LineString.class); - TLineString convTLineString = (TLineString) TConversions.Convert(module).from(lineString).toTGeometry(); - assertTrue(TLineString(module).points(new double[][]{{100, 0}, {101, 1}}).geometry().compareTo(convTLineString) == 0); - } - - @Test - public void whenConvertMultiLineStringFromGeoJsonToTGeometry() throws Exception { - MultiLineString multiLineString = new MultiLineString(); - multiLineString.add(Arrays.asList(new LngLatAlt(100, 0), new LngLatAlt(101, 1))); - TMultiLineString convTMultiLineString = (TMultiLineString) TConversions.Convert(module).from(multiLineString).toTGeometry(); - TMultiLineString compTMultiLineString = TMultiLineString(module).of - ( - TLineString(module).points(new double[][] - { - {0, 100}, - {1, 101} - }).geometry() - ).geometry(); - assertTrue(((TLineString) compTMultiLineString.getGeometryN(0)).compareTo((TLineString) convTMultiLineString.getGeometryN(0)) == 0); - } - - @Test - public void whenConvertPolygonFromGeoJsonToTGeometry() throws Exception { - Polygon polygon = geoJsonMapper.readValue("{\"type\":\"Polygon\",\"coordinates\":" - + "[[[100.0,0.0],[101.0,0.0],[101.0,1.0],[100.0,1.0],[100.0,0.0]]," - + "[[100.2,0.2],[100.8,0.2],[100.8,0.8],[100.2,0.8],[100.2,0.2]]]}", Polygon.class); - TPolygon convTPolygon = (TPolygon) TConversions.Convert(module).from(polygon).toTGeometry(); - - TPolygon compTPolygon = TPolygon(module) - .shell - ( - TLinearRing(module).ring(new double[][] - { - {0, 100}, - {0, 101}, - {1, 101}, - {1, 100}, - {0, 100} - }).geometry() - ) - .withHoles - ( - TLinearRing(module).ring(new double[][] - { - {0.2, 100.2}, - {0.2, 100.8}, - {0.8, 100.8}, - {0.8, 100.2}, - {0.2, 100.2} - }).geometry() - ) - .geometry(); - - assertEquals(compTPolygon.getNumPoints(), convTPolygon.getNumPoints()); - assertTrue(compTPolygon.shell().get().compareTo(convTPolygon.shell().get()) == 0); - } - - @Test - public void whenConvertMultiPolygonFromGeoJsonToTGeometry() throws Exception { - MultiPolygon multiPolygon = geoJsonMapper.readValue("{\"type\":\"MultiPolygon\",\"coordinates\":[[[[102.0,2.0],[103.0,2.0],[103.0,3.0],[102.0,3.0],[102.0,2.0]]]," - + "[[[100.0,0.0],[101.0,0.0],[101.0,1.0],[100.0,1.0],[100.0,0.0]]," - + "[[100.2,0.2],[100.8,0.2],[100.8,0.8],[100.2,0.8],[100.2,0.2]]]]}", MultiPolygon.class); - TMultiPolygon convTMultiPolygon = (TMultiPolygon) TConversions.Convert(module).from(multiPolygon).toTGeometry(); - assertEquals(15, convTMultiPolygon.getNumPoints()); - } - - @Test - public void whenConvertFeatureFromGeoJsonToTGeometry() throws Exception { - Feature feature = new Feature(); - feature.setGeometry(new Point(100, 0)); - TFeature convTFeature = (TFeature) TConversions.Convert(module).from(feature).toTGeometry(); - TFeature compTFeature = TFeature(module).of(TPoint(module).y(100).x(0).geometry()).geometry(); - assertTrue(convTFeature.getNumPoints() == compTFeature.getNumPoints()); - } - - @Test - public void whenConvertFeatureCollectionFromGeoJsonToTGeometry() throws Exception { - Feature f1 = new Feature(); - f1.setGeometry(geoJsonMapper.readValue("{\"type\":\"MultiPolygon\",\"coordinates\":[[[[102.0,2.0],[103.0,2.0],[103.0,3.0],[102.0,3.0],[102.0,2.0]]]," - + "[[[100.0,0.0],[101.0,0.0],[101.0,1.0],[100.0,1.0],[100.0,0.0]]," - + "[[100.2,0.2],[100.8,0.2],[100.8,0.8],[100.2,0.8],[100.2,0.2]]]]}", MultiPolygon.class)); - - Feature f2 = new Feature(); - f2.setGeometry(geoJsonMapper.readValue("{\"type\":\"LineString\",\"coordinates\":[[100.0,0.0],[101.0,1.0]]}", - LineString.class)); - - FeatureCollection featureCollection = new FeatureCollection(); - featureCollection.add(f1); - featureCollection.add(f2); - - TFeatureCollection convTFeatureCollection = (TFeatureCollection) TConversions.Convert(module).from(featureCollection).toTGeometry(); - System.out.println(convTFeatureCollection); - } - -} http://git-wip-us.apache.org/repos/asf/zest-qi4j/blob/684bb9eb/libraries/spatial/src/test/java/org/qi4j/library/spatial/formats/conversions/ConversionsFromWktTest.java ---------------------------------------------------------------------- diff --git a/libraries/spatial/src/test/java/org/qi4j/library/spatial/formats/conversions/ConversionsFromWktTest.java b/libraries/spatial/src/test/java/org/qi4j/library/spatial/formats/conversions/ConversionsFromWktTest.java new file mode 100644 index 0000000..512de6a --- /dev/null +++ b/libraries/spatial/src/test/java/org/qi4j/library/spatial/formats/conversions/ConversionsFromWktTest.java @@ -0,0 +1,33 @@ +package org.qi4j.library.spatial.formats.conversions; + +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.Test; +import org.qi4j.api.geometry.TPoint; +import org.qi4j.bootstrap.AssemblyException; +import org.qi4j.bootstrap.ModuleAssembly; +import org.qi4j.library.spatial.assembly.TGeometryAssembler; +import org.qi4j.test.AbstractQi4jTest; +import static org.junit.Assert.assertTrue; + + +public class ConversionsFromWktTest extends AbstractQi4jTest +{ + + private final String CRS_EPSG_4326 = "EPSG:4326"; + private final String CRS_EPSG_27572 = "EPSG:27572"; + private ObjectMapper GeoJsonMapper = new ObjectMapper(); + + @Override + public void assemble(ModuleAssembly module) + throws AssemblyException + { + new TGeometryAssembler().assemble(module); + } + + @Test + public void WhenConvertFromWktToTGeometry() throws Exception + { + TPoint tPoint = (TPoint) TConversions.Convert(module).from("POINT(11.57958981111 48.13905780941111 )", CRS_EPSG_27572).toTGeometry(); + assertTrue(tPoint != null); + } +} http://git-wip-us.apache.org/repos/asf/zest-qi4j/blob/684bb9eb/libraries/spatial/src/test/java/org/qi4j/library/spatial/formats/conversions/ConvertFromGeoJsonToTGeometryTest.java ---------------------------------------------------------------------- diff --git a/libraries/spatial/src/test/java/org/qi4j/library/spatial/formats/conversions/ConvertFromGeoJsonToTGeometryTest.java b/libraries/spatial/src/test/java/org/qi4j/library/spatial/formats/conversions/ConvertFromGeoJsonToTGeometryTest.java new file mode 100644 index 0000000..d63a23e --- /dev/null +++ b/libraries/spatial/src/test/java/org/qi4j/library/spatial/formats/conversions/ConvertFromGeoJsonToTGeometryTest.java @@ -0,0 +1,189 @@ +/* + * Copyright 2014 Jiri Jetmar. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + * implied. + * + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.qi4j.library.spatial.formats.conversions; + +import com.fasterxml.jackson.databind.ObjectMapper; +import org.geojson.*; +import org.junit.Ignore; +import org.junit.Test; +import org.qi4j.api.geometry.*; +import org.qi4j.bootstrap.AssemblyException; +import org.qi4j.bootstrap.ModuleAssembly; +import org.qi4j.library.spatial.assembly.TGeometryAssembler; +import org.qi4j.test.AbstractQi4jTest; + +import java.util.Arrays; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.qi4j.api.geometry.TGeometryFactory.*; + + +public class ConvertFromGeoJsonToTGeometryTest extends AbstractQi4jTest +{ + + private final String CRS_EPSG_4326_ = "EPSG:4326"; + private final String CRS_EPSG_27572 = "EPSG:27572"; + private ObjectMapper geoJsonMapper = new ObjectMapper(); + + @Override + public void assemble(ModuleAssembly module) + throws AssemblyException + { + new TGeometryAssembler().assemble(module); + } + + @Ignore("Benchmarking is not in scope for this test.") + @Test + public void whenConvertFromTGeometryToTGeometry() throws Exception + { + TPoint tPoint1 = TPoint(module).x(11.57958981111).y(48.13905780941111).geometry(); + for (int i = 0; i < 1000000; i++) + { + TPoint tPoint2 = (TPoint) TConversions.Convert(module).from(tPoint1).toTGeometry(CRS_EPSG_27572); + TPoint tPoint3 = (TPoint) TConversions.Convert(module).from(tPoint1).toTGeometry(CRS_EPSG_4326_); + } + } + + @Test + public void whenConvertPointFromGeoJsonToTGeometry() + { + TPoint tPoint = TPoint(module).y(100).x(0).geometry(); + Point gPoint = new Point(100, 0); + TPoint convTPoint = (TPoint) TConversions.Convert(module).from(gPoint).toTGeometry(); + assertTrue(tPoint.compareTo(convTPoint) == 0); + } + + @Test + public void whenConvertMultiPointFromGeoJsonToTGeometry() + { + TMultiPoint tMultiPoint = TMultiPoint(module).of + ( + TPoint(module).y(1).x(1).geometry(), + TPoint(module).y(2).x(2).geometry() + ).geometry(); + MultiPoint multiPoint = new MultiPoint(new LngLatAlt(1, 1), new LngLatAlt(2, 2)); + TMultiPoint convTMultiPoint = (TMultiPoint) TConversions.Convert(module).from(multiPoint).toTGeometry(); + assertTrue(((TPoint) tMultiPoint.getGeometryN(0)).compareTo(convTMultiPoint.getGeometryN(0)) == 0); + } + + + @Test + public void whenConvertLineStringFromGeoJsonToTGeometry() throws Exception + { + LineString lineString = geoJsonMapper.readValue("{\"type\":\"LineString\",\"coordinates\":[[100.0,0.0],[101.0,1.0]]}", + LineString.class); + TLineString convTLineString = (TLineString) TConversions.Convert(module).from(lineString).toTGeometry(); + assertTrue(TLineString(module).points(new double[][]{{100, 0}, {101, 1}}).geometry().compareTo(convTLineString) == 0); + } + + @Test + public void whenConvertMultiLineStringFromGeoJsonToTGeometry() throws Exception + { + MultiLineString multiLineString = new MultiLineString(); + multiLineString.add(Arrays.asList(new LngLatAlt(100, 0), new LngLatAlt(101, 1))); + TMultiLineString convTMultiLineString = (TMultiLineString) TConversions.Convert(module).from(multiLineString).toTGeometry(); + TMultiLineString compTMultiLineString = TMultiLineString(module).of + ( + TLineString(module).points(new double[][] + { + {0, 100}, + {1, 101} + }).geometry() + ).geometry(); + assertTrue(((TLineString) compTMultiLineString.getGeometryN(0)).compareTo((TLineString) convTMultiLineString.getGeometryN(0)) == 0); + } + + @Test + public void whenConvertPolygonFromGeoJsonToTGeometry() throws Exception + { + Polygon polygon = geoJsonMapper.readValue("{\"type\":\"Polygon\",\"coordinates\":" + + "[[[100.0,0.0],[101.0,0.0],[101.0,1.0],[100.0,1.0],[100.0,0.0]]," + + "[[100.2,0.2],[100.8,0.2],[100.8,0.8],[100.2,0.8],[100.2,0.2]]]}", Polygon.class); + TPolygon convTPolygon = (TPolygon) TConversions.Convert(module).from(polygon).toTGeometry(); + + TPolygon compTPolygon = TPolygon(module) + .shell + ( + TLinearRing(module).ring(new double[][] + { + {0, 100}, + {0, 101}, + {1, 101}, + {1, 100}, + {0, 100} + }).geometry() + ) + .withHoles + ( + TLinearRing(module).ring(new double[][] + { + {0.2, 100.2}, + {0.2, 100.8}, + {0.8, 100.8}, + {0.8, 100.2}, + {0.2, 100.2} + }).geometry() + ) + .geometry(); + + assertEquals(compTPolygon.getNumPoints(), convTPolygon.getNumPoints()); + assertTrue(compTPolygon.shell().get().compareTo(convTPolygon.shell().get()) == 0); + } + + @Test + public void whenConvertMultiPolygonFromGeoJsonToTGeometry() throws Exception + { + MultiPolygon multiPolygon = geoJsonMapper.readValue("{\"type\":\"MultiPolygon\",\"coordinates\":[[[[102.0,2.0],[103.0,2.0],[103.0,3.0],[102.0,3.0],[102.0,2.0]]]," + + "[[[100.0,0.0],[101.0,0.0],[101.0,1.0],[100.0,1.0],[100.0,0.0]]," + + "[[100.2,0.2],[100.8,0.2],[100.8,0.8],[100.2,0.8],[100.2,0.2]]]]}", MultiPolygon.class); + TMultiPolygon convTMultiPolygon = (TMultiPolygon) TConversions.Convert(module).from(multiPolygon).toTGeometry(); + assertEquals(15, convTMultiPolygon.getNumPoints()); + } + + @Test + public void whenConvertFeatureFromGeoJsonToTGeometry() throws Exception + { + Feature feature = new Feature(); + feature.setGeometry(new Point(100, 0)); + TFeature convTFeature = (TFeature) TConversions.Convert(module).from(feature).toTGeometry(); + TFeature compTFeature = TFeature(module).of(TPoint(module).y(100).x(0).geometry()).geometry(); + assertTrue(convTFeature.getNumPoints() == compTFeature.getNumPoints()); + } + + @Test + public void whenConvertFeatureCollectionFromGeoJsonToTGeometry() throws Exception + { + Feature f1 = new Feature(); + f1.setGeometry(geoJsonMapper.readValue("{\"type\":\"MultiPolygon\",\"coordinates\":[[[[102.0,2.0],[103.0,2.0],[103.0,3.0],[102.0,3.0],[102.0,2.0]]]," + + "[[[100.0,0.0],[101.0,0.0],[101.0,1.0],[100.0,1.0],[100.0,0.0]]," + + "[[100.2,0.2],[100.8,0.2],[100.8,0.8],[100.2,0.8],[100.2,0.2]]]]}", MultiPolygon.class)); + + Feature f2 = new Feature(); + f2.setGeometry(geoJsonMapper.readValue("{\"type\":\"LineString\",\"coordinates\":[[100.0,0.0],[101.0,1.0]]}", + LineString.class)); + + FeatureCollection featureCollection = new FeatureCollection(); + featureCollection.add(f1); + featureCollection.add(f2); + + TFeatureCollection convTFeatureCollection = (TFeatureCollection) TConversions.Convert(module).from(featureCollection).toTGeometry(); + } + +} http://git-wip-us.apache.org/repos/asf/zest-qi4j/blob/684bb9eb/libraries/spatial/src/test/java/org/qi4j/library/spatial/formats/data/GeoJSONSpec20080616.java ---------------------------------------------------------------------- diff --git a/libraries/spatial/src/test/java/org/qi4j/library/spatial/formats/data/GeoJSONSpec20080616.java b/libraries/spatial/src/test/java/org/qi4j/library/spatial/formats/data/GeoJSONSpec20080616.java new file mode 100644 index 0000000..803ec99 --- /dev/null +++ b/libraries/spatial/src/test/java/org/qi4j/library/spatial/formats/data/GeoJSONSpec20080616.java @@ -0,0 +1,28 @@ +package org.qi4j.library.spatial.formats.data; + +/** + * http://geojson.org/geojson-spec.html + */ +public class GeoJSONSpec20080616 +{ + + + // Version 1.0 + + public static final String LineString = "{ \"type\": \"LineString\",\r\n \"coordinates\": [ [100.0, 0.0], [101.0, 1.0] ]\r\n }"; + + public static final String Polygon = "{ \"type\": \"Polygon\",\r\n \"coordinates\": [\r\n [ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0] ]\r\n ]\r\n }"; + + public static final String Polygon_with_Holes = "{ \\\"type\\\": \\\"Polygon\\\",\\r\\n \\\"coordinates\\\": [\\r\\n [ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0] ],\\r\\n [ [100.2, 0.2], [100.8, 0.2], [100.8, 0.8], [100.2, 0.8], [100.2, 0.2] ]\\r\\n ]\\r\\n }"; + + public static final String Multipoint = "{ \\\"type\\\": \\\"MultiPoint\\\",\\r\\n \\\"coordinates\\\": [ [100.0, 0.0], [101.0, 1.0] ]\\r\\n }"; + + public static final String MultiLineString = "{ \\\"type\\\": \\\"MultiLineString\\\",\\r\\n \\\"coordinates\\\": [\\r\\n [ [100.0, 0.0], [101.0, 1.0] ],\\r\\n [ [102.0, 2.0], [103.0, 3.0] ]\\r\\n ]\\r\\n }"; + + public static final String MultiPolygon = "{ \\\"type\\\": \\\"MultiPolygon\\\",\\r\\n \\\"coordinates\\\": [\\r\\n [[[102.0, 2.0], [103.0, 2.0], [103.0, 3.0], [102.0, 3.0], [102.0, 2.0]]],\\r\\n [[[100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0]],\\r\\n [[100.2, 0.2], [100.8, 0.2], [100.8, 0.8], [100.2, 0.8], [100.2, 0.2]]]\\r\\n ]\\r\\n }"; + + public static final String GeometryCollection = "{ \\\"type\\\": \\\"GeometryCollection\\\",\\r\\n \\\"geometries\\\": [\\r\\n { \\\"type\\\": \\\"Point\\\",\\r\\n \\\"coordinates\\\": [100.0, 0.0]\\r\\n },\\r\\n { \\\"type\\\": \\\"LineString\\\",\\r\\n \\\"coordinates\\\": [ [101.0, 0.0], [102.0, 1.0] ]\\r\\n }\\r\\n ]\\r\\n }"; + + public static final String FeatureCollection = "{ \"type\": \"FeatureCollection\",\r\n \"features\": [\r\n { \"type\": \"Feature\",\r\n \"geometry\": {\"type\": \"Point\", \"coordinates\": [102.0, 0.5]},\r\n \"properties\": {\"prop0\": \"value0\"}\r\n },\r\n { \"type\": \"Feature\",\r\n \"geometry\": {\r\n \"type\": \"LineString\",\r\n \"coordinates\": [\r\n [102.0, 0.0], [103.0, 1.0], [104.0, 0.0], [105.0, 1.0]\r\n ]\r\n },\r\n \"properties\": {\r\n \"prop0\": \"value0\",\r\n \"prop1\": 0.0\r\n }\r\n },\r\n { \"type\": \"Feature\",\r\n \"geometry\": {\r\n \"type\": \"Polygon\",\r\n \"coordinates\": [\r\n [ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0],\r\n [100.0, 1.0], [100.0, 0.0] ]\r\n ]\r\n },\r\n \"properties\": {\r\n \"prop0\": \"value0\",\r\n \"prop1\": {\"this\": \"that\"}\r\n }\r\n }\r\n ]\r\n }"; + +}
