Android Open Source - android_maplib Geo Envelope






From Project

Back to project page android_maplib.

License

The source code is released under:

GNU General Public License

If you think the Android project android_maplib listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/******************************************************************************
 * Project:  NextGIS mobile/*from   w  w  w . j  av a  2  s  .c o m*/
 * Purpose:  Mobile GIS for Android.
 * Author:   Dmitry Baryshnikov (aka Bishop), polimax@mail.ru
 ******************************************************************************
 *   Copyright (C) 2014 NextGIS
 *
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 2 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ****************************************************************************/
package com.nextgis.maplib.datasource;

import com.nextgis.maplib.api.IJSONStore;
import org.json.JSONException;
import org.json.JSONObject;

import static com.nextgis.maplib.util.Constants.*;


public class GeoEnvelope
        implements IJSONStore
{
    protected Double mMinX;
    protected Double mMaxX;
    protected Double mMinY;
    protected Double mMaxY;


    public GeoEnvelope()
    {
        unInit();
    }


    protected void unInit()
    {
        mMinX = null;
        mMaxX = null;
        mMinY = null;
        mMaxY = null;
    }


    public GeoEnvelope(
            double minX,
            double maxX,
            double minY,
            double maxY)
    {
        mMinX = minX;
        mMaxX = maxX;
        mMinY = minY;
        mMaxY = maxY;
    }


    public GeoEnvelope(final GeoEnvelope env)
    {
        mMinX = env.mMinX;
        mMaxX = env.mMaxX;
        mMinY = env.mMinY;
        mMaxY = env.mMaxY;
    }


    public void setMin(
            double x,
            double y)
    {
        mMinX = x;
        mMinY = y;
    }


    public void setMax(
            double x,
            double y)
    {
        mMaxX = x;
        mMaxY = y;
    }


    public final GeoPoint getCenter()
    {
        double x = mMinX + width() / 2.0;
        double y = mMinY + height() / 2.0;
        return new GeoPoint(x, y);
    }


    public final double width()
    {
        return mMaxX - mMinX;
    }


    public final double height()
    {
        return mMaxY - mMinY;
    }


    public void adjust(double ratio)
    {
        double w = width() / 2.0;
        double h = height() / 2.0;
        double centerX = mMinX + w;
        double centerY = mMinY + h;

        double envRatio = w / h;

        if (envRatio == ratio) {
            return;
        }

        if (ratio > envRatio) //increase width
        {
            w = h * ratio;
            mMaxX = centerX + w;
            mMinX = centerX - w;
        } else                //increase height
        {
            h = w / ratio;
            mMaxY = centerY + h;
            mMinY = centerY - h;
        }
    }


    public void merge(final GeoEnvelope other)
    {
        if (isInit()) {
            mMinX = Math.min(mMinX, other.mMinX);
            mMaxX = Math.max(mMaxX, other.mMaxX);
            mMinY = Math.min(mMinY, other.mMinY);
            mMaxY = Math.max(mMaxY, other.mMaxY);
        } else {
            mMinX = other.mMinX;
            mMaxX = other.mMaxX;
            mMinY = other.mMinY;
            mMaxY = other.mMaxY;
        }
    }


    public final boolean isInit()
    {
        return mMinX != null && mMinY != null && mMaxX != null && mMaxY != null;
    }


    public void merge(
            double dfX,
            double dfY)
    {
        if (isInit()) {
            mMinX = Math.min(mMinX, dfX);
            mMaxX = Math.max(mMaxX, dfX);
            mMinY = Math.min(mMinY, dfY);
            mMaxY = Math.max(mMaxY, dfY);
        } else {
            mMinX = mMaxX = dfX;
            mMinY = mMaxY = dfY;
        }
    }


    public void intersect(final GeoEnvelope other)
    {
        if (intersects(other)) {
            if (isInit()) {
                mMinX = Math.max(mMinX, other.mMinX);
                mMaxX = Math.min(mMaxX, other.mMaxX);
                mMinY = Math.max(mMinY, other.mMinY);
                mMaxY = Math.min(mMaxY, other.mMaxY);
            } else {
                mMinX = other.mMinX;
                mMaxX = other.mMaxX;
                mMinY = other.mMinY;
                mMaxY = other.mMaxY;
            }
        } else {
            unInit();
        }
    }


    public final boolean intersects(final GeoEnvelope other)
    {
        return mMinX <= other.mMaxX && mMaxX >= other.mMinX && mMinY <= other.mMaxY &&
               mMaxY >= other.mMinY;
    }


    public final boolean contains(final GeoEnvelope other)
    {
        return mMinX <= other.mMinX && mMinY <= other.mMinY && mMaxX >= other.mMaxX &&
               mMaxY >= other.mMaxY;
    }


    public final boolean contains(final GeoPoint pt)
    {
        return mMinX <= pt.getX() && mMinY <= pt.getY() && mMaxX >= pt.getX() && mMaxY >= pt.getY();
    }


    public void offset(
            double x,
            double y)
    {
        mMinX += x;
        mMaxX += x;
        mMinY += y;
        mMaxY += y;
    }


    public void scale(double scale)
    {
        mMaxX = mMinX + width() * scale;
        mMaxY = mMinY + height() * scale;
    }


    public void fix()
    {
        if (mMinX > mMaxX) {
            double tmp = mMinX;
            mMinX = mMaxX;
            mMaxX = tmp;
        }

        if (mMinY > mMaxY) {
            double tmp = mMinY;
            mMinY = mMaxY;
            mMaxY = tmp;
        }
    }


    public String toString()
    {
        return "MinX: " + mMinX + ", MinY: " + mMinY + ", MaxX: " + mMaxX + ", MaxY: " + mMaxY;
    }


    @Override
    public JSONObject toJSON()
            throws JSONException
    {
        JSONObject oJSONBBox = new JSONObject();
        oJSONBBox.put(JSON_BBOX_MINX_KEY, getMinX());
        oJSONBBox.put(JSON_BBOX_MINY_KEY, getMinY());
        oJSONBBox.put(JSON_BBOX_MAXX_KEY, getMaxX());
        oJSONBBox.put(JSON_BBOX_MAXY_KEY, getMaxY());
        return oJSONBBox;
    }


    public final double getMinX()
    {
        return mMinX;
    }


    public void setMinX(double x)
    {
        mMinX = x;
    }


    public final double getMinY()
    {
        return mMinY;
    }


    public void setMinY(double y)
    {
        mMinY = y;
    }


    public final double getMaxX()
    {
        return mMaxX;
    }


    public void setMaxX(double x)
    {
        mMaxX = x;
    }


    public final double getMaxY()
    {
        return mMaxY;
    }


    public void setMaxY(double y)
    {
        mMaxY = y;
    }


    @Override
    public void fromJSON(JSONObject jsonObject)
            throws JSONException
    {
        setMinX(jsonObject.getDouble(JSON_BBOX_MINX_KEY));
        setMinY(jsonObject.getDouble(JSON_BBOX_MINY_KEY));
        setMaxX(jsonObject.getDouble(JSON_BBOX_MAXX_KEY));
        setMaxY(jsonObject.getDouble(JSON_BBOX_MAXY_KEY));
    }
}




Java Source Code List

com.nextgis.maplib.ApplicationTest.java
com.nextgis.maplib.api.GpsEventListener.java
com.nextgis.maplib.api.IGISApplication.java
com.nextgis.maplib.api.IJSONStore.java
com.nextgis.maplib.api.ILayerView.java
com.nextgis.maplib.api.ILayer.java
com.nextgis.maplib.api.IMapView.java
com.nextgis.maplib.api.INGWLayer.java
com.nextgis.maplib.api.IRenderer.java
com.nextgis.maplib.api.MapEventListener.java
com.nextgis.maplib.datasource.DatabaseHelper.java
com.nextgis.maplib.datasource.GeoEnvelope.java
com.nextgis.maplib.datasource.GeoGeometryCollection.java
com.nextgis.maplib.datasource.GeoGeometry.java
com.nextgis.maplib.datasource.GeoLineString.java
com.nextgis.maplib.datasource.GeoLinearRing.java
com.nextgis.maplib.datasource.GeoMultiLineString.java
com.nextgis.maplib.datasource.GeoMultiPoint.java
com.nextgis.maplib.datasource.GeoMultiPolygon.java
com.nextgis.maplib.datasource.GeoPoint.java
com.nextgis.maplib.datasource.GeoPolygon.java
com.nextgis.maplib.datasource.Geo.java
com.nextgis.maplib.datasource.NGWLayerContentProvider.java
com.nextgis.maplib.datasource.TileItem.java
com.nextgis.maplib.datasource.ngw.Connection.java
com.nextgis.maplib.datasource.ngw.Connections.java
com.nextgis.maplib.datasource.ngw.INGWResource.java
com.nextgis.maplib.datasource.ngw.LayerWithStyles.java
com.nextgis.maplib.datasource.ngw.ResourceGroup.java
com.nextgis.maplib.datasource.ngw.Resource.java
com.nextgis.maplib.display.GISDisplay.java
com.nextgis.maplib.display.Renderer.java
com.nextgis.maplib.display.SimpleFeatureRenderer.java
com.nextgis.maplib.display.SimpleLineStyle.java
com.nextgis.maplib.display.SimpleMarkerStyle.java
com.nextgis.maplib.display.Style.java
com.nextgis.maplib.display.TMSRenderer.java
com.nextgis.maplib.location.GpsEventSource.java
com.nextgis.maplib.map.LayerFactory.java
com.nextgis.maplib.map.LayerGroup.java
com.nextgis.maplib.map.Layer.java
com.nextgis.maplib.map.MapBase.java
com.nextgis.maplib.map.MapContentProviderHelper.java
com.nextgis.maplib.map.MapDrawable.java
com.nextgis.maplib.map.MapEventSource.java
com.nextgis.maplib.map.NGWRasterLayer.java
com.nextgis.maplib.map.NGWVectorLayer.java
com.nextgis.maplib.map.RemoteTMSLayer.java
com.nextgis.maplib.map.TMSLayer.java
com.nextgis.maplib.map.VectorLayer.java
com.nextgis.maplib.util.Constants.java
com.nextgis.maplib.util.DatabaseContext.java
com.nextgis.maplib.util.Feature.java
com.nextgis.maplib.util.FileUtil.java
com.nextgis.maplib.util.GeoConstants.java
com.nextgis.maplib.util.NetworkUtil.java
com.nextgis.maplib.util.SettingsConstants.java
com.nextgis.maplib.util.VectorCacheItem.java