<commit_before>/*******************************************************************************
 *
 * Copyright (c) 2015 GigaSpaces Technologies Ltd. All rights reserved
 *
 * 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.openspaces.spatial;

import com.gigaspaces.internal.utils.Assert;
import com.gigaspaces.spatial.shapes.*;
import com.gigaspaces.spatial.shapes.Circle;
import com.gigaspaces.spatial.shapes.Point;
import com.gigaspaces.spatial.shapes.Rectangle;
import com.gigaspaces.spatial.shapes.Shape;
import org.openspaces.spatial.internal.shapes.CircleImpl;
import org.openspaces.spatial.internal.shapes.PointImpl;
import org.openspaces.spatial.internal.shapes.PolygonImpl;
import org.openspaces.spatial.internal.shapes.RectangleImpl;
import com.spatial4j.core.context.SpatialContext;

import java.io.IOException;
import java.text.ParseException;
import java.util.Collection;

/**
 * Factory class for creating spatial shapes.
 *
 * @author <NAME>
 * @since 11.0
 */
public class ShapeFactory {
    /**
     * Private ctor to prevent instantiating this factory class.
     */
    private ShapeFactory() {
    }

    /**
     * Creates a Point instance.
     * @param x The X coordinate, or Longitude in geospatial contexts
     * @param y The Y coordinate, or Latitude in geospatial contexts
     * @return A new Point instance
     */
    public static Point point(double x, double y) {
        return new PointImpl(x, y);
    }

    /**
     * Creates a Circle instance
     * @param center The center of the circle
     * @param radius The radius of the circle
     * @return A new Circle instance
     */
    public static Circle circle(Point center, double radius) {
        return new CircleImpl(center, radius);
    }

    /**
     * Creates a Rectangle instance
     * @param minX The left edge of the X coordinate
     * @param maxX The right edge of the X coordinate
     * @param minY The bottom edge of the Y coordinate
     * @param maxY The top edge of the Y coordinate
     * @return A new Rectangle instance
     */
    public static Rectangle rectangle(double minX, double maxX, double minY, double maxY) {
        return new RectangleImpl(minX, maxX, minY, maxY);
    }

    /**
     * Creates a Polygon instance
     * @param first The first point
     * @param second The second point
     * @param third The third point
     * @param morePoints The rest of the points
     * @return A new Polygon instance
     */
    public static Polygon polygon(Point first, Point second, Point third, Point... morePoints) {
        Point[] points = new Point[3 + morePoints.length];
        points[0] = Assert.argumentNotNull(first, "first");
        points[1] = Assert.argumentNotNull(second, "second");
        points[2] = Assert.argumentNotNull(third, "third");
        for (int i=0 ; i < morePoints.length ; i++)
            points[i+3] = morePoints[i];

        return polygon(points);
    }

    public static Polygon polygon(Collection<Point> points) {
        return polygon(points.toArray(new Point[points.size()]));
    }


    private static Polygon polygon(Point[] points) {
        return new PolygonImpl(points);
    }

    private static SpatialContext getDefaultSpatialContext() {
        return SpatialContext.GEO;
    }

    /** Under construction */
    private static Shape fromWkt(String wkt) throws ParseException {
        try {
            return fromSpatial4JShape(getDefaultSpatialContext().getFormats().getWktReader().read(wkt));
        } catch (IOException e) {
            throw new IllegalStateException("Failed to parse WKT shape", e);
        }
    }

    /** Under construction */
    private static Shape fromGeoJson(String geoJson) throws ParseException {
        try {
            return fromSpatial4JShape(getDefaultSpatialContext().getFormats().getGeoJsonReader().read(geoJson));
        } catch (IOException e) {
            throw new IllegalStateException("Failed to parse GeoJson shape", e);
        }
    }

    private static Shape fromSpatial4JShape(com.spatial4j.core.shape.Shape shape) {
        if (shape instanceof com.spatial4j.core.shape.Point) {
            com.spatial4j.core.shape.Point point = (com.spatial4j.core.shape.Point) shape;
            return point(point.getX(), point.getY());
        }
        if (shape instanceof com.spatial4j.core.shape.Circle) {
            com.spatial4j.core.shape.Circle circle = (com.spatial4j.core.shape.Circle) shape;
            return circle(point(circle.getCenter().getX(), circle.getCenter().getY()), circle.getRadius());
        }
        if (shape instanceof com.spatial4j.core.shape.Rectangle) {
            com.spatial4j.core.shape.Rectangle rectangle = (com.spatial4j.core.shape.Rectangle) shape;
            return rectangle(rectangle.getMinX(), rectangle.getMaxX(), rectangle.getMinY(), rectangle.getMaxY());
        }
        if (shape instanceof com.spatial4j.core.shape.jts.JtsGeometry) {<commit_msg>GS-12502 - Added ability to parse wkt and geojson<commit_after>/*******************************************************************************
 *
 * Copyright (c) 2015 GigaSpaces Technologies Ltd. All rights reserved
 *
 * 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.openspaces.spatial;

import com.gigaspaces.internal.utils.Assert;
import com.gigaspaces.spatial.shapes.*;
import com.gigaspaces.spatial.shapes.Circle;
import com.gigaspaces.spatial.shapes.Point;
import com.gigaspaces.spatial.shapes.Rectangle;
import com.gigaspaces.spatial.shapes.Shape;
import org.openspaces.spatial.internal.shapes.CircleImpl;
import org.openspaces.spatial.internal.shapes.PointImpl;
import org.openspaces.spatial.internal.shapes.PolygonImpl;
import org.openspaces.spatial.internal.shapes.RectangleImpl;
import com.spatial4j.core.context.SpatialContext;

import java.io.IOException;
import java.text.ParseException;
import java.util.Collection;

/**
 * Factory class for creating spatial shapes.
 *
 * @author <NAME>
 * @since 11.0
 */
public class ShapeFactory {
    /**
     * Private ctor to prevent instantiating this factory class.
     */
    private ShapeFactory() {
    }

    /**
     * Creates a Point instance.
     * @param x The X coordinate, or Longitude in geospatial contexts
     * @param y The Y coordinate, or Latitude in geospatial contexts
     * @return A new Point instance
     */
    public static Point point(double x, double y) {
        return new PointImpl(x, y);
    }

    /**
     * Creates a Circle instance
     * @param center The center of the circle
     * @param radius The radius of the circle
     * @return A new Circle instance
     */
    public static Circle circle(Point center, double radius) {
        return new CircleImpl(center, radius);
    }

    /**
     * Creates a Rectangle instance
     * @param minX The left edge of the X coordinate
     * @param maxX The right edge of the X coordinate
     * @param minY The bottom edge of the Y coordinate
     * @param maxY The top edge of the Y coordinate
     * @return A new Rectangle instance
     */
    public static Rectangle rectangle(double minX, double maxX, double minY, double maxY) {
        return new RectangleImpl(minX, maxX, minY, maxY);
    }

    /**
     * Creates a Polygon instance from the specified points
     * @param first The first point
     * @param second The second point
     * @param third The third point
     * @param morePoints The rest of the points
     * @return A new Polygon instance
     */
    public static Polygon polygon(Point first, Point second, Point third, Point... morePoints) {
        Point[] points = new Point[3 + morePoints.length];
        points[0] = Assert.argumentNotNull(first, "first");
        points[1] = Assert.argumentNotNull(second, "second");
        points[2] = Assert.argumentNotNull(third, "third");
        for (int i=0 ; i < morePoints.length ; i++)
            points[i+3] = morePoints[i];

        return polygon(points);
    }

    /**
     * Creates a Polygon instance from the specified points
     * @param points The polygon points
     * @return A new Polygon instance
     */
    public static Polygon polygon(Collection<Point> points) {
        return polygon(points.toArray(new Point[points.size()]));
    }

    /**
     * Creates a shape from a WKT (well-known-text) string
     * @param wkt The WKT string to parse
     * @return The created shape instance
     */
    public static Shape fromWkt(String wkt) {
        try {
            return fromSpatial4JShape(getDefaultSpatialContext().getFormats().getWktReader().read(wkt));
        } catch (IOException e) {
            throw new IllegalStateException("Failed to parse WKT shape", e);
        } catch (ParseException e) {
            throw new IllegalArgumentException("Failed to parse WKT shape", e);
        }
    }

    /**
     * Creates a shape from a GeoJson string
     * @param geoJson The GeoJson string to parse
     * @return The created shape instance
     */
    public static Shape fromGeoJson(String geoJson) {
        try {
            return fromSpatial4JShape(getDefaultSpatialContext().getFormats().getGeoJsonReader().read(geoJson));
        } catch (IOException e) {
            throw new IllegalStateException("Failed to parse GeoJson shape", e);
        } catch (ParseException e) {
            throw new IllegalArgumentException("Failed to parse GeoJson shape", e);
        }
    }

    private static Polygon polygon(Point[] points) {
        return new PolygonImpl(points);
    }

    private static SpatialContext getDefaultSpatialContext() {
        return SpatialContext.GEO;
    }

    private static Shape fromSpatial4JShape(com.spatial4j.core.shape.Shape shape) {
        if (shape instanceof com.spatial4j.core.shape.Point) {
            com.spatial4j.core.shape.Point point = (com.spatial4j.core.shape.Point) shape;
            return point(point.getX(), point.getY());
        }
        if (shape instanceof com.spatial4j.core.shape.Circle) {
            com.spatial4j.core.shape.Circle circle = (com.spatial4j.core.shape.Circle) shape;
            return circle(point(circle.getCenter().getX(), circle.getCenter().getY()), circle.getRadius());
        }
        if (shape instanceof com.spatial4j.core.shape.Rectangle) {
            com.spatial4j.core.shape.Rectangle rectangle = (com.spatial4j.core.shape.Rectangle) shape;
            return rectangle(rectangle.getMinX(), rectangle.getMaxX(), rectangle.getMinY(), rectangle.getMaxY());
        }
        if (shape instanceof com.spatial4j.core.shape.jts.JtsGeometry) {
{'fraction_non_alphanumeric': 0.12121212121212122, 'fraction_numerical': 0.0058898847631242, 'mean_word_length': 3.053979238754325, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5391863', 'n_tokens_mistral': 3352, 'n_tokens_neox': 3205, 'n_words': 994}