com.nextgis.maplib.datasource.GeoGeometryFactory Java Examples

The following examples show how to use com.nextgis.maplib.datasource.GeoGeometryFactory. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: VectorLayer.java    From android_maplib with GNU Lesser General Public License v3.0 6 votes vote down vote up
protected GeoGeometry getGeometryFromQuery(
        String[] columns,
        String selection,
        SQLiteDatabase db)
{
    Cursor cursor = db.query(mPath.getName(), columns, selection, null, null, null, null);
    if (null != cursor) {
        if (cursor.moveToFirst()) {
            try {
                GeoGeometry result = GeoGeometryFactory.fromBlob(cursor.getBlob(0));
                cursor.close();
                return result;
            } catch (IOException e) {
                // e.printStackTrace();
            }
        }
        cursor.close();
    }
    return null;
}
 
Example #2
Source File: RulerOverlay.java    From android_maplibui with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void onRestoreState(Bundle bundle) {
    if (bundle.containsKey(BUNDLE_GEOMETRY))
        try {
            mRulerString = (GeoLineString) GeoGeometryFactory.fromBlob(bundle.getByteArray(BUNDLE_GEOMETRY));
        } catch (IOException e) {
            e.printStackTrace();
        }

    super.onRestoreState(bundle);
}
 
Example #3
Source File: UndoRedoOverlay.java    From android_maplibui with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void onRestoreState(Bundle bundle) {
    mHistoryState = bundle.getInt(BUNDLE_KEY_HISTORY_STATE, mHistoryState);
    for (int i = 0; i < bundle.getInt(BUNDLE_KEY_HISTORY_SIZE, mHistory.size()); i++)
        try {
            mHistory.add(GeoGeometryFactory.fromBlob(bundle.getByteArray(BUNDLE_KEY_HISTORY + i)));
        } catch (IOException e) {
            e.printStackTrace();
        }

    super.onRestoreState(bundle);
}
 
Example #4
Source File: LayerFillService.java    From android_maplibui with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public boolean execute(IProgressor progressor) {
    try {
        VectorLayer vectorLayer = (VectorLayer) mLayer;
        if (null == vectorLayer)
            return false;
        File meta = new File(mPath.getParentFile(), NGFP_META);

        if (meta.exists()) {
            String jsonText = FileUtil.readFromFile(meta);
            JSONObject metaJson = new JSONObject(jsonText);
            //read fields
            List<Field> fields = NGWUtil.getFieldsFromJson(metaJson.getJSONArray(NGWUtil.NGWKEY_FIELDS));
            //read geometry type
            String geomTypeString = metaJson.getString("geometry_type");
            int geomType = GeoGeometryFactory.typeFromString(geomTypeString);
            vectorLayer.create(geomType, fields);

            if (GeoJSONUtil.isGeoJsonHasFeatures(mPath)) {
                //read SRS -- not need as we will be fill layer with 3857
                JSONObject srs = metaJson.getJSONObject(NGWUtil.NGWKEY_SRS);
                int nSRS = srs.getInt(NGWUtil.NGWKEY_ID);
                vectorLayer.fillFromGeoJson(mPath, nSRS, progressor);
            }
        } else
            vectorLayer.createFromGeoJson(mPath, progressor); // should never get there
    } catch (IOException | JSONException | SQLiteException | NGException | ClassCastException e) {
        e.printStackTrace();
        setError(e, progressor);
        notifyError(mProgressMessage);
        return false;
    }

    if (mDeletePath)
        FileUtil.deleteRecursive(mPath);

    return true;
}
 
Example #5
Source File: NGWUtil.java    From android_maplib with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static Feature readNGWFeature(
        JsonReader reader,
        List<Field> fields,
        int nSRS)
        throws IOException, IllegalStateException, NumberFormatException, OutOfMemoryError
{
    final Feature feature = new Feature(Constants.NOT_FOUND, fields);

    reader.beginObject();
    while (reader.hasNext()) {
        String name = reader.nextName();
        if (name.equals(NGWUtil.NGWKEY_ID)) {
            feature.setId(reader.nextLong());
        } else if (name.equals(NGWUtil.NGWKEY_GEOM)) {
            String wkt = reader.nextString();
            GeoGeometry geom = GeoGeometryFactory.fromWKT(wkt, nSRS);
            geom.setCRS(nSRS);
            if (nSRS != GeoConstants.CRS_WEB_MERCATOR) {
                geom.project(GeoConstants.CRS_WEB_MERCATOR);
            }
            feature.setGeometry(geom);
        } else if (name.equals(NGWUtil.NGWKEY_FIELDS)) {
            readNGWFeatureFields(feature, reader, fields);
        } else if (name.equals(NGWUtil.NGWKEY_EXTENSIONS)) {
            if (reader.peek() != JsonToken.NULL) {
                readNGWFeatureAttachments(feature, reader);
            }
        } else {
            reader.skipValue();
        }
    }
    reader.endObject();
    return feature;
}
 
Example #6
Source File: NGWVectorLayer.java    From android_maplib with GNU Lesser General Public License v3.0 4 votes vote down vote up
protected String cursorToJson(Cursor cursor)
        throws JSONException, IOException
{
    JSONObject rootObject = new JSONObject();
    if (0 != (mSyncType & Constants.SYNC_ATTRIBUTES)) {
        JSONObject valueObject = new JSONObject();
        for (int i = 0; i < cursor.getColumnCount(); i++) {
            String name = cursor.getColumnName(i);
            if (name.equals(Constants.FIELD_ID) || name.equals(Constants.FIELD_GEOM)) {
                continue;
            }

            Field field = mFields.get(cursor.getColumnName(i));
            if (null == field) {
                continue;
            }

            switch (field.getType()) {
                case GeoConstants.FTReal:
                    valueObject.put(name, cursor.getFloat(i));
                    break;
                case GeoConstants.FTInteger:
                    valueObject.put(name, cursor.getInt(i));
                    break;
                case GeoConstants.FTString:
                    String stringVal = cursor.getString(i);
                    if (null != stringVal && !stringVal.equals("null")) {
                        valueObject.put(name, stringVal);
                    }
                    break;
                case GeoConstants.FTDateTime:
                    TimeZone timeZoneDT = TimeZone.getDefault();
                    timeZoneDT.setRawOffset(0); // set to UTC
                    Calendar calendarDT = Calendar.getInstance(timeZoneDT);
                    calendarDT.setTimeInMillis(cursor.getLong(i));
                    JSONObject jsonDateTime = new JSONObject();
                    jsonDateTime.put("year", calendarDT.get(Calendar.YEAR));
                    jsonDateTime.put("month", calendarDT.get(Calendar.MONTH) + 1);
                    jsonDateTime.put("day", calendarDT.get(Calendar.DAY_OF_MONTH));
                    jsonDateTime.put("hour", calendarDT.get(Calendar.HOUR_OF_DAY));
                    jsonDateTime.put("minute", calendarDT.get(Calendar.MINUTE));
                    jsonDateTime.put("second", calendarDT.get(Calendar.SECOND));
                    valueObject.put(name, jsonDateTime);
                    break;
                case GeoConstants.FTDate:
                    TimeZone timeZoneD = TimeZone.getDefault();
                    timeZoneD.setRawOffset(0); // set to UTC
                    Calendar calendarD = Calendar.getInstance(timeZoneD);
                    calendarD.setTimeInMillis(cursor.getLong(i));
                    JSONObject jsonDate = new JSONObject();
                    jsonDate.put("year", calendarD.get(Calendar.YEAR));
                    jsonDate.put("month", calendarD.get(Calendar.MONTH) + 1);
                    jsonDate.put("day", calendarD.get(Calendar.DAY_OF_MONTH));
                    valueObject.put(name, jsonDate);
                    break;
                case GeoConstants.FTTime:
                    TimeZone timeZoneT = TimeZone.getDefault();
                    timeZoneT.setRawOffset(0); // set to UTC
                    Calendar calendarT = Calendar.getInstance(timeZoneT);
                    calendarT.setTimeInMillis(cursor.getLong(i));
                    JSONObject jsonTime = new JSONObject();
                    jsonTime.put("hour", calendarT.get(Calendar.HOUR_OF_DAY));
                    jsonTime.put("minute", calendarT.get(Calendar.MINUTE));
                    jsonTime.put("second", calendarT.get(Calendar.SECOND));
                    valueObject.put(name, jsonTime);
                    break;
                default:
                    break;
            }
        }
        rootObject.put(NGWUtil.NGWKEY_FIELDS, valueObject);
    }

    if (0 != (mSyncType & Constants.SYNC_GEOMETRY)) {
        //may be found geometry in cache by id is faster
        GeoGeometry geometry = GeoGeometryFactory.fromBlob(
                cursor.getBlob(cursor.getColumnIndex(Constants.FIELD_GEOM)));

        geometry.setCRS(GeoConstants.CRS_WEB_MERCATOR);
        if (mCRS != GeoConstants.CRS_WEB_MERCATOR)
            geometry.project(mCRS);

        rootObject.put(NGWUtil.NGWKEY_GEOM, geometry.toWKT(true));
        //rootObject.put("id", cursor.getLong(cursor.getColumnIndex(FIELD_ID)));
    }

    return rootObject.toString();
}
 
Example #7
Source File: VectorLayer.java    From android_maplib with GNU Lesser General Public License v3.0 4 votes vote down vote up
public void rebuildCache(IProgressor progressor)
{
    if (null != progressor) {
        progressor.setMessage(mContext.getString(R.string.rebuild_cache));
    }

    String columns[] = {FIELD_ID, FIELD_GEOM};
    Cursor cursor = query(columns, null, null, null, null);
    if (null != cursor) {
        if (cursor.moveToFirst()) {
            if (null != progressor) {
                progressor.setMax(cursor.getCount());
            }

            mIsCacheRebuilding = true;
            mCache = createNewCache();
            int counter = 0;
            do {
                GeoGeometry geometry = null;
                try {
                    geometry = GeoGeometryFactory.fromBlob(cursor.getBlob(1));
                } catch (IOException e) {
                    e.printStackTrace();
                }

                if (null != geometry) {
                    long rowId = cursor.getLong(0);
                    mCache.addItem(rowId, geometry.getEnvelope());
                }

                if (null != progressor) {
                    if (progressor.isCanceled()) {
                        break;
                    }
                    progressor.setValue(++counter);
                    progressor.setMessage(
                            mContext.getString(R.string.process_features) + ": " + counter);
                }

            } while (cursor.moveToNext());

            mIsCacheRebuilding = false;
        }
        cursor.close();
        save();
    }
}
 
Example #8
Source File: NGWUtil.java    From android_maplib with GNU Lesser General Public License v3.0 4 votes vote down vote up
public static HttpResponse createNewLayer(
        Connection connection,
        VectorLayer layer,
        long parentId,
        String keyName)
{
    JSONObject payload = new JSONObject();
    try {
        JSONObject resource = new JSONObject();
        resource.put(NGWKEY_CLS, NGWKEY_VECTOR_LAYER);
        JSONObject id = new JSONObject();
        id.put(JSON_ID_KEY, parentId);
        resource.put(NGWKEY_PARENT, id);
        resource.put(JSON_DISPLAY_NAME, layer.getName());
        if (keyName != null) {
            resource.put(NGWKEY_KEYNAME, keyName);
        }
        payload.put(JSON_RESOURCE_KEY, resource);

        JSONObject vectorLayer = new JSONObject();
        JSONObject srs = new JSONObject();
        srs.put(JSON_ID_KEY, GeoConstants.CRS_WEB_MERCATOR);
        vectorLayer.put(NGWKEY_SRS, srs);
        vectorLayer.put(
                NGWKEY_GEOMETRY_TYPE, GeoGeometryFactory.typeToString(layer.getGeometryType()));
        JSONArray fields = new JSONArray();
        for (Field field : layer.getFields()) {
            JSONObject current = new JSONObject();
            current.put(NGWKEY_KEYNAME, field.getName());
            current.put(NGWKEY_DISPLAY_NAME, field.getAlias());
            current.put(NGWKEY_DATATYPE, LayerUtil.typeToString(field.getType()));
            fields.put(current);
        }
        vectorLayer.put(NGWKEY_FIELDS, fields);
        payload.put(NGWKEY_VECTOR_LAYER, vectorLayer);
    } catch (JSONException e) {
        e.printStackTrace();
        return new HttpResponse(500);
    }

    return createNewResource(layer.getContext(), connection, payload);
}