Java Code Examples for com.nextgis.maplib.datasource.GeoGeometry#setCRS()

The following examples show how to use com.nextgis.maplib.datasource.GeoGeometry#setCRS() . 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: 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 2
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();
}