com.nextgis.maplib.datasource.Feature Java Examples

The following examples show how to use com.nextgis.maplib.datasource.Feature. 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: MapFragment.java    From android_gisapp with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void onSaveInstanceState(Bundle outState)
{
    super.onSaveInstanceState(outState);
    outState.putBoolean(BUNDLE_KEY_IS_MEASURING, mRulerOverlay.isMeasuring());
    outState.putInt(KEY_MODE, mMode);
    outState.putInt(BUNDLE_KEY_LAYER, null == mSelectedLayer ? Constants.NOT_FOUND : mSelectedLayer.getId());

    Feature feature = mEditLayerOverlay.getSelectedFeature();
    outState.putLong(BUNDLE_KEY_FEATURE_ID, null == feature ? Constants.NOT_FOUND : feature.getId());

    if (null != feature && feature.getGeometry() != null) {
        try {
            outState.putByteArray(BUNDLE_KEY_SAVED_FEATURE, feature.getGeometry().toBlob());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
 
Example #2
Source File: VectorLayer.java    From android_maplib with GNU Lesser General Public License v3.0 6 votes vote down vote up
public AttachItem getNewTempAttach(Feature feature)
{
    long featureId = feature.getId();
    AttachItem attachItem = new AttachItem("" + NOT_FOUND, "", "", "");
    Uri uri = insertTempAttach(featureId, attachItem);

    if (uri == null) {
        return null;
    }

    String attachId = uri.getLastPathSegment();
    attachItem = getAttach("" + featureId, attachId);
    feature.addAttachment(attachItem);

    return attachItem;
}
 
Example #3
Source File: NGWUtil.java    From android_maplib with GNU Lesser General Public License v3.0 6 votes vote down vote up
private static void readNGWFeatureAttachments(Feature feature, JsonReader reader)
        throws IOException
{
    //add extensions
    reader.beginObject();
    while (reader.hasNext()) {
        String name = reader.nextName();
        if (name.equals("attachment") && reader.peek() != JsonToken.NULL) {
            reader.beginArray();
            while (reader.hasNext()) {
                readNGWFeatureAttachment(feature, reader);
            }
            reader.endArray();
        } else {
            reader.skipValue();
        }
    }

    reader.endObject();
}
 
Example #4
Source File: VectorLayer.java    From android_maplib with GNU Lesser General Public License v3.0 6 votes vote down vote up
public void createFeatureBatch(
        final Feature feature,
        final SQLiteDatabase db)
        throws SQLiteException
{
    if (null == feature.getGeometry() || !checkGeometryType(feature)) {
        return;
    }

    final ContentValues values = getFeatureContentValues(feature);

    long rowId = db.insert(mPath.getName(), "", values);
    if (rowId != Constants.NOT_FOUND) {
        //update bbox
        cacheGeometryEnvelope(rowId, feature.getGeometry());
    }
}
 
Example #5
Source File: SimpleFeatureRenderer.java    From android_maplib with GNU Lesser General Public License v3.0 6 votes vote down vote up
protected Style applyField(Style style, long featureId) {
    if (style instanceof ITextStyle) {
        String fieldValue = ((ITextStyle) style).getField();

        if (fieldValue != null) {
            Feature feature = ((VectorLayer) getLayer()).getFeature(featureId);
            if (fieldValue.equals(FIELD_ID))
                fieldValue = feature.getId() + "";
            else
                fieldValue = feature.getFieldValueAsString(fieldValue);

            ((ITextStyle) style).setText(fieldValue);
        }
    }

    return style;
}
 
Example #6
Source File: VectorLayer.java    From android_maplib with GNU Lesser General Public License v3.0 6 votes vote down vote up
protected Uri insertTempFeature(Feature feature)
{
    Uri uri = Uri.parse("content://" + mAuthority + "/" + mPath.getName());

    uri = uri.buildUpon()
            .appendQueryParameter(URI_PARAMETER_TEMP, Boolean.TRUE.toString())
            .build();

    Uri result = insert(uri, feature.getContentValues(false));

    if (result == null) {
        Log.d(TAG, "insert feature failed");
        return null;
    }

    return result;
}
 
Example #7
Source File: NGWVectorLayer.java    From android_maplib with GNU Lesser General Public License v3.0 6 votes vote down vote up
protected void proceedAddedFeatures(List<Feature> added, String authority, String changeTableName) {
    if (added != null) {
        for (Feature remoteFeature : added) {
            Cursor cursor = query(null, Constants.FIELD_ID + " = " + remoteFeature.getId(), null, null, null);
            boolean hasFeature = false;
            if (cursor != null) {
                if (cursor.moveToFirst()) {
                    compareFeature(cursor, authority, remoteFeature, changeTableName);
                    hasFeature = true;
                }
                cursor.close();
            }

            if (!hasFeature)
                createNewFeature(remoteFeature, authority);
        }
    }
}
 
Example #8
Source File: VectorLayer.java    From android_maplib with GNU Lesser General Public License v3.0 6 votes vote down vote up
protected Void doInBackground(Void... params) {
    try {
        NGWVectorLayer layer = (NGWVectorLayer) MapDrawable.getInstance().getLayerByPathName(getPath().getName());
        FeatureChanges.initialize(layer.getChangeTableName());
        List<Long> ids = query(null);
        for (Long id : ids) {
            Feature feature = getFeatureWithAttaches(id);
            layer.addChange(feature.getId(), CHANGE_OPERATION_NEW);
            Map<String, AttachItem> attaches = feature.getAttachments();
            for (AttachItem attach : attaches.values())
                layer.addChange(feature.getId(), Long.parseLong(attach.getAttachId()), CHANGE_OPERATION_NEW);
        }

        Pair<Integer, Integer> ver = NGWUtil.getNgwVersion(mContext, layer.getAccountName());
        layer.sync(mAuthority, ver, new SyncResult());
    } catch (Exception ignored) { }

    return null;
}
 
Example #9
Source File: VectorLayer.java    From android_maplib with GNU Lesser General Public License v3.0 5 votes vote down vote up
public Feature getNewTempFeature()
{
    Feature feature = new Feature(NOT_FOUND, getFields());
    Uri uri = insertTempFeature(feature);

    if (uri == null) {
        return null;
    }

    long featureId = Long.parseLong(uri.getLastPathSegment());
    feature.setId(featureId);

    return feature;
}
 
Example #10
Source File: MapFragment.java    From android_gisapp with GNU General Public License v3.0 5 votes vote down vote up
protected void createPointFromOverlay() {
    mEditLayerOverlay.setSelectedFeature(new Feature());
    mEditLayerOverlay.getSelectedFeature().setGeometry(new GeoPoint());
    setMode(MODE_EDIT);
    mUndoRedoOverlay.clearHistory();
    mEditLayerOverlay.createPointFromOverlay();
    mEditLayerOverlay.setHasEdits(true);
    mUndoRedoOverlay.saveToHistory(mEditLayerOverlay.getSelectedFeature());
}
 
Example #11
Source File: VectorLayer.java    From android_maplib with GNU Lesser General Public License v3.0 5 votes vote down vote up
public int updateFeatureWithFlags(Feature feature)
{
    boolean tempFlag = hasFeatureTempFlag(feature.getId());
    boolean notSyncFlag = hasFeatureNotSyncFlag(feature.getId());

    if (!tempFlag && !notSyncFlag) {
        return 0;
    }

    String layerPathName = mPath.getName();

    Uri uri =
            Uri.parse("content://" + mAuthority + "/" + layerPathName + "/" + feature.getId());

    if (tempFlag) {
        uri = uri.buildUpon()
                .appendQueryParameter(URI_PARAMETER_TEMP, Boolean.TRUE.toString())
                .build();
    }

    if (notSyncFlag) {
        uri = uri.buildUpon()
                .appendQueryParameter(URI_PARAMETER_NOT_SYNC, Boolean.TRUE.toString())
                .build();
    }

    return update(uri, feature.getContentValues(false), null, null);
}
 
Example #12
Source File: VectorLayer.java    From android_maplib with GNU Lesser General Public License v3.0 5 votes vote down vote up
public int updateAttachWithFlags(
        Feature feature,
        AttachItem attachItem)
{
    String layerPathName = mPath.getName();
    long featureIdL = feature.getId();
    long attachIdL = Long.parseLong(attachItem.getAttachId());

    boolean tempFlag = hasAttachTempFlag(featureIdL, attachIdL);
    boolean notSyncFlag = hasAttachNotSyncFlag(featureIdL, attachIdL);

    if (!tempFlag && !notSyncFlag) {
        return 0;
    }

    Uri uri = Uri.parse("content://" + mAuthority + "/" + layerPathName + "/" + featureIdL + "/"
            + Constants.URI_ATTACH + "/" + attachIdL);

    if (tempFlag) {
        uri = uri.buildUpon()
                .appendQueryParameter(URI_PARAMETER_TEMP, Boolean.TRUE.toString())
                .build();
    }

    if (notSyncFlag) {
        uri = uri.buildUpon()
                .appendQueryParameter(URI_PARAMETER_NOT_SYNC, Boolean.TRUE.toString())
                .build();
    }

    return update(uri, attachItem.getContentValues(false), null, null);
}
 
Example #13
Source File: VectorLayer.java    From android_maplib with GNU Lesser General Public License v3.0 5 votes vote down vote up
public int updateAttach(Feature feature, AttachItem attachItem) {
    String layerPathName = mPath.getName();
    long featureIdL = feature.getId();
    long attachIdL = Long.parseLong(attachItem.getAttachId());
    Uri uri = Uri.parse("content://" + mAuthority + "/" + layerPathName + "/" + featureIdL + "/" + Constants.URI_ATTACH + "/" + attachIdL);
    return update(uri, attachItem.getContentValues(false), null, null);
}
 
Example #14
Source File: VectorLayer.java    From android_maplib with GNU Lesser General Public License v3.0 5 votes vote down vote up
public int updateFeatureWithAttachesWithFlags(Feature feature)
{
    int res = updateFeatureWithFlags(feature);

    long featureIdL = feature.getId();

    Map<String, AttachItem> attaches = getAttachMap("" + featureIdL);
    if (null != attaches) {
        for (AttachItem attachItem : attaches.values()) {
            res += updateAttachWithFlags(feature, attachItem);
        }
    }

    return res;
}
 
Example #15
Source File: VectorLayer.java    From android_maplib with GNU Lesser General Public License v3.0 5 votes vote down vote up
public int updateFeatureWithAttaches(Feature feature) {
    int res = updateFeature(feature);
    long featureIdL = feature.getId();

    Map<String, AttachItem> attaches = getAttachMap("" + featureIdL);
    if (null != attaches)
        for (AttachItem attachItem : attaches.values())
            res += updateAttach(feature, attachItem);

    return res;
}
 
Example #16
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 #17
Source File: NGWUtil.java    From android_maplib with GNU Lesser General Public License v3.0 5 votes vote down vote up
private static void readNGWDate(Feature feature, JsonReader reader, String fieldName)
        throws IOException
{
    reader.beginObject();
    int nYear = 1900;
    int nMonth = 1;
    int nDay = 1;
    int nHour = 0;
    int nMinute = 0;
    int nSecond = 0;
    while (reader.hasNext()) {
        String name = reader.nextName();
        if (name.equals(NGWUtil.NGWKEY_YEAR)) {
            nYear = reader.nextInt();
        } else if (name.equals(NGWUtil.NGWKEY_MONTH)) {
            nMonth = reader.nextInt();
        } else if (name.equals(NGWUtil.NGWKEY_DAY)) {
            nDay = reader.nextInt();
        } else if (name.equals(NGWUtil.NGWKEY_HOUR)) {
            nHour = reader.nextInt();
        } else if (name.equals(NGWUtil.NGWKEY_MINUTE)) {
            nMinute = reader.nextInt();
        } else if (name.equals(NGWUtil.NGWKEY_SECOND)) {
            nSecond = reader.nextInt();
        } else {
            reader.skipValue();
        }
    }

    TimeZone timeZone = TimeZone.getDefault();
    timeZone.setRawOffset(0); // set to UTC
    Calendar calendar = new GregorianCalendar(timeZone);
    calendar.set(nYear, nMonth - 1, nDay, nHour, nMinute, nSecond);
    calendar.set(Calendar.MILLISECOND, 0); // we must to reset millis
    feature.setFieldValue(fieldName, calendar.getTimeInMillis());

    reader.endObject();
}
 
Example #18
Source File: NGWUtil.java    From android_maplib with GNU Lesser General Public License v3.0 5 votes vote down vote up
private static void readNGWFeatureAttachment(Feature feature, JsonReader reader)
        throws IOException
{
    reader.beginObject();
    String attachId = "";
    String name = "";
    String mime = "";
    String descriptionText = "";
    while (reader.hasNext()) {
        String keyName = reader.nextName();
        if (reader.peek() == JsonToken.NULL) {
            reader.skipValue();
            continue;
        }

        if (keyName.equals(NGWUtil.NGWKEY_ID)) {
            attachId += reader.nextLong();
        } else if (keyName.equals(NGWUtil.NGWKEY_NAME)) {
            name += reader.nextString();
        } else if (keyName.equals(NGWUtil.NGWKEY_MIME)) {
            mime += reader.nextString();
        } else if (keyName.equals(NGWUtil.NGWKEY_DESCRIPTION)) {
            descriptionText += reader.nextString();
        } else {
            reader.skipValue();
        }
    }
    AttachItem item = new AttachItem(attachId, name, mime, descriptionText);
    feature.addAttachment(item);

    reader.endObject();
}
 
Example #19
Source File: EditLayerOverlay.java    From android_maplibui with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void setSelectedFeature(long featureId) {
    clearDrawItems();

    if (mLayer != null && featureId > Constants.NOT_FOUND) {
        mFeature = new Feature(featureId, mLayer.getFields());
        mFeature.setGeometry(mLayer.getGeometryForId(featureId));
    } else
        mFeature = null;

    updateMap();
}
 
Example #20
Source File: VectorLayer.java    From android_maplib with GNU Lesser General Public License v3.0 5 votes vote down vote up
public Feature cursorToFeature(Cursor cursor)
{
    Feature out = new Feature((long) Constants.NOT_FOUND, getFields());
    out.fromCursor(cursor);
    //add extensions to feature
    out.addAttachments(getAttachMap("" + out.getId()));
    return out;
}
 
Example #21
Source File: VectorLayer.java    From android_maplib with GNU Lesser General Public License v3.0 5 votes vote down vote up
public long createFeature(Feature feature)
        throws SQLiteException
{
    if (null == feature.getGeometry() || !checkGeometryType(feature)) {
        return NOT_FOUND;
    }

    if (!mCacheLoaded) {
        reloadCache();
    }

    // check if such id already used
    // maybe was added previous session
    if (mCache.getItem(feature.getId()) != null) {
        return NOT_FOUND;
    }

    MapContentProviderHelper map = (MapContentProviderHelper) MapBase.getInstance();
    SQLiteDatabase db = map.getDatabase(false);

    final ContentValues values = getFeatureContentValues(feature);

    if (Constants.DEBUG_MODE) {
        Log.d(TAG, "Inserting " + values);
    }
    long rowId = db.insert(mPath.getName(), "", values);
    if (rowId != Constants.NOT_FOUND) {
        //update bbox
        cacheGeometryEnvelope(rowId, feature.getGeometry());
        save();
    }

    return rowId;
}
 
Example #22
Source File: VectorLayer.java    From android_maplib with GNU Lesser General Public License v3.0 5 votes vote down vote up
protected ContentValues getFeatureContentValues(Feature feature)
{
    final ContentValues values = feature.getContentValues(true);
    try {
        prepareGeometry(values);
    } catch (IOException | ClassNotFoundException e) {
        e.printStackTrace();
    }
    return values;
}
 
Example #23
Source File: VectorLayer.java    From android_maplib with GNU Lesser General Public License v3.0 5 votes vote down vote up
public Feature getFeatureWithAttaches(long featureId)
{
    Feature feature = getFeature(featureId);

    if (null != feature) {
        feature.addAttachments(getAttachMap("" + feature.getId()));
    }

    return feature;
}
 
Example #24
Source File: NGWVectorLayer.java    From android_maplib with GNU Lesser General Public License v3.0 5 votes vote down vote up
protected void readFeatures(JsonReader reader, List<Feature> features) throws IOException, IllegalStateException, NumberFormatException, OutOfMemoryError {
    reader.beginArray();
    while (reader.hasNext()) {
        final Feature feature = NGWUtil.readNGWFeature(reader, getFields(), mCRS);
        if (feature.getGeometry() == null || !feature.getGeometry().isValid())
            continue;
        features.add(feature);
    }
    reader.endArray();
}
 
Example #25
Source File: NGWVectorLayer.java    From android_maplib with GNU Lesser General Public License v3.0 5 votes vote down vote up
protected void createNewFeature(Feature remoteFeature, String authority) {
    ContentValues values = remoteFeature.getContentValues(true);
    Uri uri = Uri.parse("content://" + authority + "/" + getPath().getName());
    //prevent add changes and events
    uri = uri.buildUpon().fragment(NO_SYNC).build();
    Uri newFeatureUri = insert(uri, values);
    if (Constants.DEBUG_MODE) {
        Log.d(Constants.TAG, "Add new feature from server - " + newFeatureUri.toString());
    }
}
 
Example #26
Source File: NGWVectorLayer.java    From android_maplib with GNU Lesser General Public License v3.0 5 votes vote down vote up
protected void proceedDeletedFeatures(List<Feature> deleted, String changeTableName) {
    List<Long> deleteItems = new ArrayList<>();
    if (deleted != null) {
        for (Feature remoteFeature : deleted)
            deleteItems.add(remoteFeature.getId());

        deleteFeatures(deleteItems);
    }
}
 
Example #27
Source File: NGWVectorLayer.java    From android_maplib with GNU Lesser General Public License v3.0 5 votes vote down vote up
protected void proceedChangedFeatures(List<Feature> changed, String authority, String changeTableName) {
    if (changed != null) {
        for (Feature remoteFeature : changed) {
            Cursor cursor = query(null, Constants.FIELD_ID + " = " + remoteFeature.getId(), null, null, null);
            if (cursor != null) {
                if (cursor.moveToFirst()) {
                    compareFeature(cursor, authority, remoteFeature, changeTableName);
                }
                cursor.close();
            }
        }
    }
}
 
Example #28
Source File: UndoRedoOverlay.java    From android_maplibui with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void saveToHistory(Feature feature) {
    if (null == feature || null == feature.getGeometry())
        return;

    for (int i = mHistory.size() - 1; i > mHistoryState; i--)
        mHistory.remove(i);

    if (mHistory.size() >= MAX_UNDO + 1)
        mHistory.removeFirst();

    mHistoryState++;
    mHistory.add(feature.getGeometry().copy());
    mFeature.setGeometry(mHistory.getLast());
    defineUndoRedo();
}
 
Example #29
Source File: EditLayerOverlay.java    From android_maplibui with GNU Lesser General Public License v3.0 4 votes vote down vote up
public void setSelectedFeature(Feature feature) {
    clearDrawItems();
    mFeature = feature;
    updateMap();
}
 
Example #30
Source File: EditLayerOverlay.java    From android_maplibui with GNU Lesser General Public License v3.0 4 votes vote down vote up
public Feature getSelectedFeature() {
    return mFeature;
}