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

com.cocoahero.android.geojson.Polygon Maven / Gradle / Ivy

package com.cocoahero.android.geojson;

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

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Parcel;
import android.os.Parcelable;

public class Polygon extends Geometry {

    // ------------------------------------------------------------------------
    // Instance Variables
    // ------------------------------------------------------------------------

    private final List mRings = new ArrayList();

    // ------------------------------------------------------------------------
    // Constructors
    // ------------------------------------------------------------------------

    public Polygon() {
        // Default Constructor
    }

    public Polygon(Ring ring) {
        this.addRing(ring);
    }

    public Polygon(JSONObject json) {
        super(json);

        this.setRings(json.optJSONArray(JSON_COORDINATES));
    }

    public Polygon(JSONArray rings) {
        this.setRings(rings);
    }

    // ------------------------------------------------------------------------
    // Parcelable Interface
    // ------------------------------------------------------------------------

    public static final Parcelable.Creator CREATOR = new Creator() {
        @Override
        public Polygon createFromParcel(Parcel in) {
            return (Polygon) readParcel(in);
        }

        @Override
        public Polygon[] newArray(int size) {
            return new Polygon[size];
        }
    };

    // ------------------------------------------------------------------------
    // Public Methods
    // ------------------------------------------------------------------------

    public void addRing(Ring ring) {
        this.mRings.add(ring);
    }

    public void removeRing(Ring ring) {
        this.mRings.remove(ring);
    }

    public List getRings() {
        return this.mRings;
    }

    public void setRings(JSONArray rings) {
        this.mRings.clear();
        if (rings != null) {
            for (int i = 0; i < rings.length(); i++) {
                JSONArray ringJSON = rings.optJSONArray(i);
                if (ringJSON != null) {
                    this.mRings.add(new Ring(ringJSON));
                }
            }
        }
    }

    public void setRings(List rings) {
        this.mRings.clear();
        if (rings != null) {
            this.mRings.addAll(rings);
        }
    }

    @Override
    public String getType() {
        return GeoJSON.TYPE_POLYGON;
    }

    @Override
    public JSONObject toJSON() throws JSONException {
        JSONObject json = super.toJSON();

        JSONArray rings = new JSONArray();
        for (Ring ring : this.mRings) {
            rings.put(ring.toJSON());
        }
        json.put(JSON_COORDINATES, rings);

        return json;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy