com.nextgis.maplib.util.NGException Java Examples

The following examples show how to use com.nextgis.maplib.util.NGException. 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: LayerFillService.java    From android_maplibui with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public boolean execute(IProgressor progressor) {
    try {
        VectorLayer vectorLayer = (VectorLayer) mLayer;
        if(null == vectorLayer)
            return false;

        vectorLayer.createFromGeoJson(mUri, progressor);
    } catch (IOException | JSONException | SQLiteException | NGException | ClassCastException e) {
        e.printStackTrace();
        setError(e, progressor);
        notifyError(mProgressMessage);
        return false;
    }

    return true;
}
 
Example #2
Source File: LayerFillService.java    From android_maplibui with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public boolean execute(IProgressor progressor) {
    try {
        TMSLayer tmsLayer = (TMSLayer) mLayer;
        if (null == tmsLayer)
            return false;

        if (mIsNgrc)
            tmsLayer.fillFromNgrc(mUri, progressor);
        else
            tmsLayer.fillFromZip(mUri, progressor);
    } catch (IOException | NGException | RuntimeException e) {
        e.printStackTrace();
        setError(e, progressor);
        notifyError(mProgressMessage);
        return false;
    }

    return true;
}
 
Example #3
Source File: NGWLookupTable.java    From android_maplib with GNU Lesser General Public License v3.0 6 votes vote down vote up
protected void fillFromNGW(Map<String, String> dataMap, IProgressor progressor) throws IOException, NGException, JSONException {
    if (!mNet.isNetworkAvailable()) { //return tile from cache
        throw new NGException(getContext().getString(R.string.error_network_unavailable));
    }

    if(null != progressor){
        progressor.setMessage(getContext().getString(R.string.message_loading));
    }

    Log.d(Constants.TAG, "download layer " + getName());
    HttpResponse response =
            NetworkUtil.get(NGWUtil.getResourceUrl(mCacheUrl, mRemoteId), mCacheLogin,
                    mCachePassword, false);
    if (!response.isOk()) {
        throw new NGException(NetworkUtil.getError(mContext, response.getResponseCode()));
    }
    JSONObject geoJSONObject = new JSONObject(response.getResponseBody());
    JSONObject lookupTable = geoJSONObject.getJSONObject("lookup_table");
    JSONObject itemsObject = lookupTable.getJSONObject("items");
    for (Iterator<String> iter = itemsObject.keys(); iter.hasNext(); ) {
        String key = iter.next();
        String value = itemsObject.getString(key);
        dataMap.put(key, value);
    }
}
 
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 fillFromGeoJson(
        Uri uri,
        int srs,
        IProgressor progressor)
        throws IOException, JSONException, NGException, SQLiteException
{
    InputStream inputStream = mContext.getContentResolver().openInputStream(uri);
    if (inputStream == null) {
        throw new NGException(mContext.getString(R.string.error_download_data));
    }

    if (null != progressor) {
        progressor.setMessage(mContext.getString(R.string.create_features));
    }
    GeoJSONUtil.fillLayerFromGeoJSONStream(this, inputStream, srs, progressor);
}
 
Example #5
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 #6
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 {
        NGWVectorLayer ngwVectorLayer = (NGWVectorLayer) mLayer;
        if (null == ngwVectorLayer)
            return false;

        for (String id : mLookupIds) {
            NGWLookupTable table = new NGWLookupTable(mLayer.getContext(), mLayerGroup.createLayerStorage());
            table.setAccountName(((NGWVectorLayer) mLayer).getAccountName());
            table.setRemoteId(Long.parseLong(id));
            table.setSyncType(Constants.SYNC_ALL);
            table.setName(getText(R.string.layer_lookuptable) + " #" + id);
            table.fillFromNGW(null);
            mLayerGroup.addLayer(table);
        }

        ngwVectorLayer.createFromNGW(progressor);
    } catch (JSONException | IOException | SQLiteException | NGException | ClassCastException e) {
        e.printStackTrace();
        setError(e, progressor);
        notifyError(mProgressMessage);
        return false;
    }

    return true;
}
 
Example #7
Source File: TMSLayer.java    From android_maplib with GNU Lesser General Public License v3.0 5 votes vote down vote up
protected void fillFromZipInt(Uri uri, IProgressor progressor) throws IOException, NGException, RuntimeException {
    InputStream inputStream;
    String url = uri.toString();
    if (NetworkUtil.isValidUri(url))
        inputStream = new URL(url).openStream();
    else
        inputStream = mContext.getContentResolver().openInputStream(uri);

    if (inputStream == null)
        throw new NGException(mContext.getString(R.string.error_download_data));

    int streamSize = inputStream.available();
    if (null != progressor)
        progressor.setMax(streamSize);

    int increment = 0;
    byte[] buffer = new byte[Constants.IO_BUFFER_SIZE];

    ZipInputStream zis = new ZipInputStream(inputStream);
    ZipEntry ze;
    while ((ze = zis.getNextEntry()) != null) {
        FileUtil.unzipEntry(zis, ze, buffer, mPath);
        increment += ze.getCompressedSize();
        zis.closeEntry();
        if (null != progressor) {
            if(progressor.isCanceled())
                return;
            progressor.setValue(increment);
            progressor.setMessage(getContext().getString(R.string.processed) + " " + increment + " " + getContext().getString(R.string.of) + " " + streamSize);
        }
    }
}
 
Example #8
Source File: VectorLayer.java    From android_maplib with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void createFromGeoJson(
        Uri uri,
        IProgressor progressor)
        throws IOException, JSONException, NGException, SQLiteException
{
    InputStream inputStream, is;
    String url = uri.toString();
    if (NetworkUtil.isValidUri(url)) {
        inputStream = new URL(url).openStream();
        is = new URL(url).openStream();
    } else {
        inputStream = mContext.getContentResolver().openInputStream(uri);
        is = mContext.getContentResolver().openInputStream(uri);
    }

    if (inputStream == null) {
        throw new NGException(mContext.getString(R.string.error_download_data));
    }

    if (null != progressor) {
        progressor.setMessage(mContext.getString(R.string.message_opening));
        progressor.setIndeterminate(true);
    }

    boolean isWGS84 = GeoJSONUtil.readGeoJSONCRS(is, getContext());
    GeoJSONUtil.createLayerFromGeoJSONStream(this, inputStream, progressor, isWGS84);
}
 
Example #9
Source File: VectorLayer.java    From android_maplib with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void createFromGeoJson(
        File path,
        IProgressor progressor)
        throws IOException, JSONException, NGException
{
    if (null != progressor) {
        progressor.setMessage(mContext.getString(R.string.message_opening));
        progressor.setIndeterminate(true);
    }

    FileInputStream inputStream = new FileInputStream(path);
    FileInputStream is = new FileInputStream(path);
    boolean isWGS84 = GeoJSONUtil.readGeoJSONCRS(is, getContext());
    GeoJSONUtil.createLayerFromGeoJSONStream(this, inputStream, progressor, isWGS84);
}
 
Example #10
Source File: VectorLayer.java    From android_maplib with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void fillFromGeoJson(
        File path,
        int srs,
        IProgressor progressor)
        throws IOException, JSONException, NGException
{
    if (null != progressor) {
        progressor.setMessage(mContext.getString(R.string.create_features));
    }

    FileInputStream inputStream = new FileInputStream(path);
    GeoJSONUtil.fillLayerFromGeoJSONStream(this, inputStream, srs, progressor);
}
 
Example #11
Source File: NGWLookupTable.java    From android_maplib with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public void sync(String authority, Pair<Integer, Integer> ver, SyncResult syncResult) {
    if (0 != (mSyncType & Constants.SYNC_NONE)) {
        return;
    }

    Map<String, String> remoteData = new Hashtable<>();

    try {
        fillFromNGW(remoteData, null);
    } catch (IOException | NGException | JSONException e) {
        e.printStackTrace();
        String locMsg = e.getLocalizedMessage();
        if(locMsg != null) {
            Log.d(Constants.TAG, locMsg);
        }
        syncResult.stats.numParseExceptions++;
        return;
    }

    if(remoteData.size() != mData.size()){
        mData.clear();
        mData.putAll(remoteData);
        save();
        Log.d(Constants.TAG, "Update lookup table " + getName() + " from server");
        return;
    }

    // check key and values
    boolean isSame = true;
    for (Map.Entry<String, String> entry : remoteData.entrySet()) {
        if(!mData.get(entry.getKey()).equals(entry.getValue())){
            isSame = false;
            break;
        }
    }

    if(!isSame){
        mData.clear();
        mData.putAll(remoteData);
        save();
        Log.d(Constants.TAG, "Update lookup table " + getName() + " from server");
        return;
    }
}
 
Example #12
Source File: NGWLookupTable.java    From android_maplib with GNU Lesser General Public License v3.0 4 votes vote down vote up
public void fillFromNGW(IProgressor progressor) throws JSONException, NGException, IOException {
    if(null == mData)
        mData = new LinkedHashMap<>();
    fillFromNGW(mData, progressor);
}
 
Example #13
Source File: LocalTMSLayer.java    From android_maplib with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public void fillFromZip(Uri uri, IProgressor progressor) throws IOException, NGException, RuntimeException {
    fillFromZipInt(uri, progressor);

    int nMaxLevel = 0;
    int nMinLevel = 512;
    final File[] zoomLevels = mPath.listFiles();

    if (zoomLevels == null)
        throw new NGException("Invalid content or zip structure");

    if(null != progressor){
        progressor.setMax(zoomLevels.length);
        progressor.setValue(0);
        progressor.setMessage(mContext.getString(R.string.message_opening));
    }

    int counter = 0;

    for (File zoomLevel : zoomLevels) {
        if(null != progressor) {
            if(progressor.isCanceled())
                return;
            progressor.setValue(counter++);
            progressor.setMessage(getContext().getString(R.string.processed) + " " + counter + " " + getContext().getString(R.string.of) + " " + zoomLevels.length);

        }

        if(zoomLevel.getName().equals(Constants.CONFIG) || !zoomLevel.isDirectory()){
            continue;
        }

        int nMaxX = 0;
        int nMinX = 10000000;
        int nMaxY = 0;
        int nMinY = 10000000;

        int nLevelZ = Integer.parseInt(zoomLevel.getName());
        if (nLevelZ > nMaxLevel) {
            nMaxLevel = nLevelZ;
        }
        if (nLevelZ < nMinLevel) {
            nMinLevel = nLevelZ;
        }
        final File[] levelsX = zoomLevel.listFiles();

        boolean bFirstTurn = true;
        for (File inLevelX : levelsX) {

            int nX = Integer.parseInt(inLevelX.getName());
            if (nX > nMaxX) {
                nMaxX = nX;
            }
            if (nX < nMinX) {
                nMinX = nX;
            }

            final File[] levelsY = inLevelX.listFiles();

            if (bFirstTurn) {
                for (File inLevelY : levelsY) {
                    String sLevelY = inLevelY.getName();

                    //Log.d(TAG, sLevelY);
                    int nY = Integer.parseInt(
                            sLevelY.replace(TILE_EXT, ""));
                    if (nY > nMaxY) {
                        nMaxY = nY;
                    }
                    if (nY < nMinY) {
                        nMinY = nY;
                    }
                }
                bFirstTurn = false;
            }
        }
        addLimits(nLevelZ, nMaxX, nMaxY, nMinX, nMinY);
    }

    save();
}
 
Example #14
Source File: TMSLayer.java    From android_maplib with GNU Lesser General Public License v3.0 4 votes vote down vote up
public void fillFromZip(Uri uri, IProgressor progressor) throws IOException, NumberFormatException, SecurityException, NGException {
    fillFromZipInt(uri, progressor);
    save();
}
 
Example #15
Source File: TMSLayer.java    From android_maplib with GNU Lesser General Public License v3.0 4 votes vote down vote up
public void fillFromNgrc(Uri uri, IProgressor progressor) throws IOException, NumberFormatException, SecurityException, NGException {
    fillFromZipInt(uri, progressor);
    load();
}
 
Example #16
Source File: MainApplication.java    From android_gisapp with GNU General Public License v3.0 4 votes vote down vote up
public void initBaseLayers() {
    if (mMap.getLayerByPathName(LAYER_OSM) == null) {
        //add OpenStreetMap layer
        String layerName = getString(R.string.osm);
        String layerURL = SettingsConstantsUI.OSM_URL;
        final RemoteTMSLayerUI layer = new RemoteTMSLayerUI(getApplicationContext(), mMap.createLayerStorage(LAYER_OSM));
        layer.setName(layerName);
        layer.setURL(layerURL);
        layer.setTMSType(TMSTYPE_OSM);
        layer.setVisible(true);
        layer.setMinZoom(GeoConstants.DEFAULT_MIN_ZOOM);
        layer.setMaxZoom(19);

        mMap.addLayer(layer);
        mMap.moveLayer(0, layer);

        new Handler().post(new Runnable() {
            @Override
            public void run() {
                try {
                    layer.fillFromZip(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.mapnik), null);
                } catch (IOException | NGException | RuntimeException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    // create empty layers for first experimental editing
    List<Field> fields = new ArrayList<>(2);
    fields.add(new Field(GeoConstants.FTInteger, "FID", "FID"));
    fields.add(new Field(GeoConstants.FTString, "TEXT", "TEXT"));

    if (mMap.getLayerByPathName(LAYER_A) == null)
        mMap.addLayer(createEmptyVectorLayer(getString(R.string.points_for_edit), LAYER_A, GeoConstants.GTPoint, fields));
    if (mMap.getLayerByPathName(LAYER_B) == null)
        mMap.addLayer(createEmptyVectorLayer(getString(R.string.lines_for_edit), LAYER_B, GeoConstants.GTLineString, fields));
    if (mMap.getLayerByPathName(LAYER_C) == null)
        mMap.addLayer(createEmptyVectorLayer(getString(R.string.polygons_for_edit), LAYER_C, GeoConstants.GTPolygon, fields));

    mMap.save();
}