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

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

There is a newer version: 9.2.1
Show newest version
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 MultiPolygon extends Geometry {

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

    private final List mPolygons = new ArrayList();

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

    public MultiPolygon() {
        // Default Constructor
    }

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

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

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

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

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

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

    public void addPolygon(Polygon polygon) {
        this.mPolygons.add(polygon);
    }

    public void removePolygon(Polygon polygon) {
        this.mPolygons.remove(polygon);
    }

    public List getPolygons() {
        return this.mPolygons;
    }

    public void setPolygons(JSONArray polygons) {
        this.mPolygons.clear();
        if (polygons != null) {
            for (int i = 0; i < polygons.length(); i++) {
                JSONArray polyJSON = polygons.optJSONArray(i);
                if (polyJSON != null) {
                    this.mPolygons.add(new Polygon(polyJSON));
                }
            }
        }
    }

    public void setPolygons(List polygons) {
        this.mPolygons.clear();
        if (polygons != null) {
            this.mPolygons.addAll(polygons);
        }
    }

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

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

        JSONArray polygons = new JSONArray();
        for (Polygon polygon : this.mPolygons) {
            JSONArray ringJSON = new JSONArray();
            for (Ring ring : polygon.getRings()) {
                ringJSON.put(ring.toJSON());
            }
            polygons.put(ringJSON);
        }
        json.put(JSON_COORDINATES, polygons);

        return json;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy