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

com.backendless.persistence.Polygon Maven / Gradle / Ivy

The newest version!
package com.backendless.persistence;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;


public class Polygon extends Geometry
{
  public static final String GEOJSON_TYPE = "Polygon";
  public static final String WKT_TYPE = GEOJSON_TYPE.toUpperCase();

  private LineString boundary;
  private final List holes;


  public Polygon( LineString boundary )
  {
    this( boundary, null, SpatialReferenceSystemEnum.DEFAULT );
  }

  public Polygon( List boundary, List holes )
  {
    this( boundary, holes, SpatialReferenceSystemEnum.DEFAULT );
  }

  public Polygon( List boundary, List holes, SpatialReferenceSystemEnum srs )
  {
    this( new LineString( boundary, srs ), holes, srs );
  }

  public Polygon( LineString boundary, List holes )
  {
    this( boundary, holes, SpatialReferenceSystemEnum.DEFAULT );
  }

  public Polygon( LineString boundary, List holes, SpatialReferenceSystemEnum srs )
  {
    super( srs );

    if( boundary == null )
      throw new IllegalArgumentException( "The 'shell' shouldn't be null." );

    if( holes != null && !holes.isEmpty() )
    {
      this.holes = new ArrayList<>( holes );
    }
    else
      this.holes = new ArrayList<>();

    this.boundary = boundary;
  }

  public LineString getBoundary()
  {
    return this.boundary;
  }

  public Polygon setBoundary( LineString boundary )
  {
    this.boundary = boundary;
    return this;
  }

  public List getHoles()
  {
    return this.holes;
  }

  public Polygon setHoles( List holes )
  {
    this.holes.clear();
    this.holes.addAll( holes );

    return this;
  }

  @Override
  public String getGeojsonType()
  {
    return Polygon.GEOJSON_TYPE;
  }

  @Override
  public String getWktType()
  {
    return Polygon.WKT_TYPE;
  }

  @Override
  String jsonCoordinatePairs()
  {
    StringBuilder sb = new StringBuilder();

    sb.append( '[' );
    sb.append( this.getBoundary().jsonCoordinatePairs() ).append( "," );

    if( this.getHoles() != null )
    {
      for( LineString ls : this.getHoles() )
        sb.append( ls.jsonCoordinatePairs() ).append( "," );
    }

    sb.setCharAt( sb.length() - 1, ']' );
    return sb.toString();
  }

  @Override
  String wktCoordinatePairs()
  {
    StringBuilder sb = new StringBuilder();

    sb.append( '(' ).append( this.getBoundary().wktCoordinatePairs() ).append( ")," );

    if( this.getHoles() != null )
    {
      for( LineString ls : this.getHoles() )
        sb.append( '(' ).append( ls.wktCoordinatePairs() ).append( ")," );
    }

    sb.deleteCharAt( sb.length() - 1 );
    return sb.toString();
  }

  @Override
  public boolean equals( Object o )
  {
    if( this == o )
      return true;
    if( !(o instanceof Polygon) )
      return false;
    Polygon polygon = (Polygon) o;
    return Objects.equals( this.boundary, polygon.boundary ) && Objects.equals( holes, polygon.holes ) && srs == polygon.srs;
  }

  @Override
  public int hashCode()
  {
    return Objects.hash( boundary, holes, srs );
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy