All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.lucene.geo.Polygon Maven / Gradle / Ivy

There is a newer version: 9.11.1
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.apache.lucene.geo;

import java.text.ParseException;
import java.util.Arrays;

/**
 * Represents a closed polygon on the earth's surface.  You can either construct the Polygon directly yourself with {@code double[]}
 * coordinates, or use {@link Polygon#fromGeoJSON} if you have a polygon already encoded as a
 * GeoJSON string.
 * 

* NOTES: *

    *
  1. Coordinates must be in clockwise order, except for holes. Holes must be in counter-clockwise order. *
  2. The polygon must be closed: the first and last coordinates need to have the same values. *
  3. The polygon must not be self-crossing, otherwise may result in unexpected behavior. *
  4. All latitude/longitude values must be in decimal degrees. *
  5. Polygons cannot cross the 180th meridian. Instead, use two polygons: one on each side. *
  6. For more advanced GeoSpatial indexing and query operations see the {@code spatial-extras} module *
* @lucene.experimental */ public final class Polygon { private final double[] polyLats; private final double[] polyLons; private final Polygon[] holes; /** minimum latitude of this polygon's bounding box area */ public final double minLat; /** maximum latitude of this polygon's bounding box area */ public final double maxLat; /** minimum longitude of this polygon's bounding box area */ public final double minLon; /** maximum longitude of this polygon's bounding box area */ public final double maxLon; /** * Creates a new Polygon from the supplied latitude/longitude array, and optionally any holes. */ public Polygon(double[] polyLats, double[] polyLons, Polygon... holes) { if (polyLats == null) { throw new IllegalArgumentException("polyLats must not be null"); } if (polyLons == null) { throw new IllegalArgumentException("polyLons must not be null"); } if (holes == null) { throw new IllegalArgumentException("holes must not be null"); } if (polyLats.length != polyLons.length) { throw new IllegalArgumentException("polyLats and polyLons must be equal length"); } if (polyLats.length != polyLons.length) { throw new IllegalArgumentException("polyLats and polyLons must be equal length"); } if (polyLats.length < 4) { throw new IllegalArgumentException("at least 4 polygon points required"); } if (polyLats[0] != polyLats[polyLats.length-1]) { throw new IllegalArgumentException("first and last points of the polygon must be the same (it must close itself): polyLats[0]=" + polyLats[0] + " polyLats[" + (polyLats.length-1) + "]=" + polyLats[polyLats.length-1]); } if (polyLons[0] != polyLons[polyLons.length-1]) { throw new IllegalArgumentException("first and last points of the polygon must be the same (it must close itself): polyLons[0]=" + polyLons[0] + " polyLons[" + (polyLons.length-1) + "]=" + polyLons[polyLons.length-1]); } for (int i = 0; i < polyLats.length; i++) { GeoUtils.checkLatitude(polyLats[i]); GeoUtils.checkLongitude(polyLons[i]); } for (int i = 0; i < holes.length; i++) { Polygon inner = holes[i]; if (inner.holes.length > 0) { throw new IllegalArgumentException("holes may not contain holes: polygons may not nest."); } } this.polyLats = polyLats.clone(); this.polyLons = polyLons.clone(); this.holes = holes.clone(); // compute bounding box double minLat = Double.POSITIVE_INFINITY; double maxLat = Double.NEGATIVE_INFINITY; double minLon = Double.POSITIVE_INFINITY; double maxLon = Double.NEGATIVE_INFINITY; for (int i = 0;i < polyLats.length; i++) { minLat = Math.min(polyLats[i], minLat); maxLat = Math.max(polyLats[i], maxLat); minLon = Math.min(polyLons[i], minLon); maxLon = Math.max(polyLons[i], maxLon); } this.minLat = minLat; this.maxLat = maxLat; this.minLon = minLon; this.maxLon = maxLon; } /** Returns a copy of the internal latitude array */ public double[] getPolyLats() { return polyLats.clone(); } /** Returns a copy of the internal longitude array */ public double[] getPolyLons() { return polyLons.clone(); } /** Returns a copy of the internal holes array */ public Polygon[] getHoles() { return holes.clone(); } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + Arrays.hashCode(holes); result = prime * result + Arrays.hashCode(polyLats); result = prime * result + Arrays.hashCode(polyLons); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Polygon other = (Polygon) obj; if (!Arrays.equals(holes, other.holes)) return false; if (!Arrays.equals(polyLats, other.polyLats)) return false; if (!Arrays.equals(polyLons, other.polyLons)) return false; return true; } @Override public String toString() { StringBuilder sb = new StringBuilder(); for (int i = 0; i < polyLats.length; i++) { sb.append("[") .append(polyLats[i]) .append(", ") .append(polyLons[i]) .append("] "); } if (holes.length > 0) { sb.append(", holes="); sb.append(Arrays.toString(holes)); } return sb.toString(); } /** Parses a standard GeoJSON polygon string. The type of the incoming GeoJSON object must be a Polygon or MultiPolygon, optionally * embedded under a "type: Feature". A Polygon will return as a length 1 array, while a MultiPolygon will be 1 or more in length. * *

See the GeoJSON specification. */ public static Polygon[] fromGeoJSON(String geojson) throws ParseException { return new SimpleGeoJSONPolygonParser(geojson).parse(); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy