Search in sources :

Example 1 with GeometryCollection

use of mil.nga.sf.GeometryCollection in project geopackage-android-map by ngageoint.

the class GoogleMapShapeConverter method toGeometry.

/**
 * Convert a GoogleMapShape to a Geometry
 *
 * @param shape google map shape
 * @return geometry
 */
public Geometry toGeometry(GoogleMapShape shape) {
    Geometry geometry = null;
    Object shapeObject = shape.getShape();
    switch(shape.getGeometryType()) {
        case POINT:
            LatLng point = null;
            switch(shape.getShapeType()) {
                case LAT_LNG:
                    point = (LatLng) shapeObject;
                    break;
                case MARKER_OPTIONS:
                    MarkerOptions markerOptions = (MarkerOptions) shapeObject;
                    point = markerOptions.getPosition();
                    break;
                case MARKER:
                    Marker marker = (Marker) shapeObject;
                    point = marker.getPosition();
                    break;
                default:
                    throw new GeoPackageException("Not a valid " + shape.getGeometryType().getName() + " shape type: " + shape.getShapeType());
            }
            if (point != null) {
                geometry = toPoint(point);
            }
            break;
        case LINESTRING:
        case CIRCULARSTRING:
            List<LatLng> lineStringPoints = null;
            switch(shape.getShapeType()) {
                case POLYLINE_OPTIONS:
                    PolylineOptions polylineOptions = (PolylineOptions) shapeObject;
                    lineStringPoints = polylineOptions.getPoints();
                    break;
                case POLYLINE:
                    Polyline polyline = (Polyline) shapeObject;
                    lineStringPoints = polyline.getPoints();
                    break;
                case POLYLINE_MARKERS:
                    PolylineMarkers polylineMarkers = (PolylineMarkers) shapeObject;
                    if (!polylineMarkers.isValid()) {
                        throw new GeoPackageException(PolylineMarkers.class.getSimpleName() + " is not valid to create " + shape.getGeometryType().getName());
                    }
                    if (!polylineMarkers.isDeleted()) {
                        lineStringPoints = getPointsFromMarkers(polylineMarkers.getMarkers());
                    }
                    break;
                default:
                    throw new GeoPackageException("Not a valid " + shape.getGeometryType().getName() + " shape type: " + shape.getShapeType());
            }
            if (lineStringPoints != null) {
                switch(shape.getGeometryType()) {
                    case LINESTRING:
                        geometry = toLineString(lineStringPoints);
                        break;
                    case CIRCULARSTRING:
                        geometry = toCircularString(lineStringPoints);
                        break;
                    default:
                        throw new GeoPackageException("Unhandled " + shape.getGeometryType().getName());
                }
            }
            break;
        case POLYGON:
            List<LatLng> polygonPoints = null;
            List<List<LatLng>> holePointList = null;
            switch(shape.getShapeType()) {
                case POLYGON_OPTIONS:
                    PolygonOptions polygonOptions = (PolygonOptions) shapeObject;
                    polygonPoints = polygonOptions.getPoints();
                    holePointList = polygonOptions.getHoles();
                    break;
                case POLYGON:
                    com.google.android.gms.maps.model.Polygon polygon = (com.google.android.gms.maps.model.Polygon) shapeObject;
                    polygonPoints = polygon.getPoints();
                    holePointList = polygon.getHoles();
                    break;
                case POLYGON_MARKERS:
                    PolygonMarkers polygonMarkers = (PolygonMarkers) shapeObject;
                    if (!polygonMarkers.isValid()) {
                        throw new GeoPackageException(PolygonMarkers.class.getSimpleName() + " is not valid to create " + shape.getGeometryType().getName());
                    }
                    if (!polygonMarkers.isDeleted()) {
                        polygonPoints = getPointsFromMarkers(polygonMarkers.getMarkers());
                        holePointList = new ArrayList<>();
                        for (PolygonHoleMarkers hole : polygonMarkers.getHoles()) {
                            if (!hole.isDeleted()) {
                                List<LatLng> holePoints = getPointsFromMarkers(hole.getMarkers());
                                holePointList.add(holePoints);
                            }
                        }
                    }
                    break;
                default:
                    throw new GeoPackageException("Not a valid " + shape.getGeometryType().getName() + " shape type: " + shape.getShapeType());
            }
            if (polygonPoints != null) {
                geometry = toPolygon(polygonPoints, holePointList);
            }
            break;
        case MULTIPOINT:
            List<LatLng> multiPoints = null;
            switch(shape.getShapeType()) {
                case MULTI_LAT_LNG:
                    MultiLatLng multiLatLng = (MultiLatLng) shapeObject;
                    multiPoints = multiLatLng.getLatLngs();
                    break;
                case MULTI_MARKER:
                    MultiMarker multiMarker = (MultiMarker) shapeObject;
                    multiPoints = getPointsFromMarkers(multiMarker.getMarkers());
                    break;
                default:
                    throw new GeoPackageException("Not a valid " + shape.getGeometryType().getName() + " shape type: " + shape.getShapeType());
            }
            if (multiPoints != null) {
                geometry = toMultiPoint(multiPoints);
            }
            break;
        case MULTILINESTRING:
        case COMPOUNDCURVE:
            switch(shape.getShapeType()) {
                case MULTI_POLYLINE_OPTIONS:
                    MultiPolylineOptions multiPolylineOptions = (MultiPolylineOptions) shapeObject;
                    switch(shape.getGeometryType()) {
                        case MULTILINESTRING:
                            geometry = toMultiLineStringFromOptions(multiPolylineOptions);
                            break;
                        case COMPOUNDCURVE:
                            geometry = toCompoundCurveFromOptions(multiPolylineOptions);
                            break;
                        default:
                            throw new GeoPackageException("Unhandled " + shape.getGeometryType().getName());
                    }
                    break;
                case MULTI_POLYLINE:
                    MultiPolyline multiPolyline = (MultiPolyline) shapeObject;
                    switch(shape.getGeometryType()) {
                        case MULTILINESTRING:
                            geometry = toMultiLineString(multiPolyline.getPolylines());
                            break;
                        case COMPOUNDCURVE:
                            geometry = toCompoundCurve(multiPolyline.getPolylines());
                            break;
                        default:
                            throw new GeoPackageException("Unhandled " + shape.getGeometryType().getName());
                    }
                    break;
                case MULTI_POLYLINE_MARKERS:
                    MultiPolylineMarkers multiPolylineMarkers = (MultiPolylineMarkers) shapeObject;
                    if (!multiPolylineMarkers.isValid()) {
                        throw new GeoPackageException(MultiPolylineMarkers.class.getSimpleName() + " is not valid to create " + shape.getGeometryType().getName());
                    }
                    if (!multiPolylineMarkers.isDeleted()) {
                        List<List<LatLng>> multiPolylineMarkersList = new ArrayList<>();
                        for (PolylineMarkers polylineMarkers : multiPolylineMarkers.getPolylineMarkers()) {
                            if (!polylineMarkers.isDeleted()) {
                                multiPolylineMarkersList.add(getPointsFromMarkers(polylineMarkers.getMarkers()));
                            }
                        }
                        switch(shape.getGeometryType()) {
                            case MULTILINESTRING:
                                geometry = toMultiLineStringFromList(multiPolylineMarkersList);
                                break;
                            case COMPOUNDCURVE:
                                geometry = toCompoundCurveFromList(multiPolylineMarkersList);
                                break;
                            default:
                                throw new GeoPackageException("Unhandled " + shape.getGeometryType().getName());
                        }
                    }
                    break;
                default:
                    throw new GeoPackageException("Not a valid " + shape.getGeometryType().getName() + " shape type: " + shape.getShapeType());
            }
            break;
        case MULTIPOLYGON:
            switch(shape.getShapeType()) {
                case MULTI_POLYGON_OPTIONS:
                    MultiPolygonOptions multiPolygonOptions = (MultiPolygonOptions) shapeObject;
                    geometry = toMultiPolygonFromOptions(multiPolygonOptions);
                    break;
                case MULTI_POLYGON:
                    mil.nga.geopackage.map.geom.MultiPolygon multiPolygon = (mil.nga.geopackage.map.geom.MultiPolygon) shapeObject;
                    geometry = toMultiPolygon(multiPolygon.getPolygons());
                    break;
                case MULTI_POLYGON_MARKERS:
                    MultiPolygonMarkers multiPolygonMarkers = (MultiPolygonMarkers) shapeObject;
                    if (!multiPolygonMarkers.isValid()) {
                        throw new GeoPackageException(MultiPolygonMarkers.class.getSimpleName() + " is not valid to create " + shape.getGeometryType().getName());
                    }
                    if (!multiPolygonMarkers.isDeleted()) {
                        List<Polygon> multiPolygonMarkersList = new ArrayList<>();
                        for (PolygonMarkers polygonMarkers : multiPolygonMarkers.getPolygonMarkers()) {
                            if (!polygonMarkers.isDeleted()) {
                                List<LatLng> multiPolygonPoints = getPointsFromMarkers(polygonMarkers.getMarkers());
                                List<List<LatLng>> multiPolygonHolePoints = new ArrayList<>();
                                for (PolygonHoleMarkers hole : polygonMarkers.getHoles()) {
                                    if (!hole.isDeleted()) {
                                        List<LatLng> holePoints = getPointsFromMarkers(hole.getMarkers());
                                        multiPolygonHolePoints.add(holePoints);
                                    }
                                }
                                multiPolygonMarkersList.add(toPolygon(multiPolygonPoints, multiPolygonHolePoints));
                            }
                        }
                        geometry = createMultiPolygon(multiPolygonMarkersList);
                    }
                    break;
                default:
                    throw new GeoPackageException("Not a valid " + shape.getGeometryType().getName() + " shape type: " + shape.getShapeType());
            }
            break;
        case POLYHEDRALSURFACE:
        case TIN:
        case TRIANGLE:
            throw new GeoPackageException("Unsupported GeoPackage type: " + shape.getGeometryType());
        case GEOMETRYCOLLECTION:
            @SuppressWarnings("unchecked") List<GoogleMapShape> shapeList = (List<GoogleMapShape>) shapeObject;
            GeometryCollection<Geometry> geometryCollection = new GeometryCollection<>(false, false);
            for (GoogleMapShape shapeListItem : shapeList) {
                Geometry subGeometry = toGeometry(shapeListItem);
                if (subGeometry != null) {
                    geometryCollection.addGeometry(subGeometry);
                }
            }
            if (geometryCollection.numGeometries() > 0) {
                geometry = geometryCollection;
            }
            break;
        default:
    }
    return geometry;
}
Also used : PolygonOptions(com.google.android.gms.maps.model.PolygonOptions) ArrayList(java.util.ArrayList) PolylineOptions(com.google.android.gms.maps.model.PolylineOptions) GeometryCollection(mil.nga.sf.GeometryCollection) ArrayList(java.util.ArrayList) List(java.util.List) LatLng(com.google.android.gms.maps.model.LatLng) MultiPolygon(mil.nga.sf.MultiPolygon) CurvePolygon(mil.nga.sf.CurvePolygon) Polygon(mil.nga.sf.Polygon) MarkerOptions(com.google.android.gms.maps.model.MarkerOptions) Marker(com.google.android.gms.maps.model.Marker) Geometry(mil.nga.sf.Geometry) MultiPolygon(mil.nga.sf.MultiPolygon) Polyline(com.google.android.gms.maps.model.Polyline) GeoPackageException(mil.nga.geopackage.GeoPackageException)

Aggregations

LatLng (com.google.android.gms.maps.model.LatLng)1 Marker (com.google.android.gms.maps.model.Marker)1 MarkerOptions (com.google.android.gms.maps.model.MarkerOptions)1 PolygonOptions (com.google.android.gms.maps.model.PolygonOptions)1 Polyline (com.google.android.gms.maps.model.Polyline)1 PolylineOptions (com.google.android.gms.maps.model.PolylineOptions)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 GeoPackageException (mil.nga.geopackage.GeoPackageException)1 CurvePolygon (mil.nga.sf.CurvePolygon)1 Geometry (mil.nga.sf.Geometry)1 GeometryCollection (mil.nga.sf.GeometryCollection)1 MultiPolygon (mil.nga.sf.MultiPolygon)1 Polygon (mil.nga.sf.Polygon)1