2

To consume geojson data from a REST HAL CORS webservice, in client side i'm using ExtJs 5, but to map data having geometries i'm stuck between choosing geoExt2 which has a branch based on extjs5 and openlayers 2.3, or working directly with extjs5 and openlayers 3, for consuming data till the moment i'm using rest proxy in extjs, but geoext proxy seems not to be agile because it's based on OpenLayers.Protocol.HTTP which do not support REST, is there any suggestions how to use extjs5 and ol3 with rest.

1

With OL3, it is quite easy to read GeoJSON, using ol.source.Vector.

I'm using Ext.direct, but it is the same pattern if you use the regular Ext.Ajax.request call. On the callback, you use the readFeatures method.

To read a GeoJSON layer:

    var vectorSource = new ol.source.Vector({
        format: new ol.format.GeoJSON(),
        loader: function (extent, resolution, projection) {
            Server.Plantas.Pedidos.asGeoJson({
                gid: 0
            }, function (result, event) {
                if (result.success) {
                    if (result.data.features) {
                        var features = (new ol.format.GeoJSON()).readFeatures(result.data);
                        vectorSource.addFeatures(features);
                    }
                } else {
                    console.log('Problem', result.message);
                }
            });
        }
    });

    var vectorJSON = new ol.layer.Vector({
        source: vectorSource
    });
    map.addLayer(vectorJSON);

On the server side, I use a SQL query to return the GeoJSON from the database:

    var sql = '';
    sql += 'SELECT row_to_json(fc) as geojson ';
    sql += "FROM ( SELECT 'FeatureCollection' As type, array_to_json(array_agg(f)) As features ";
    sql += 'FROM ( ';
    //-- query
    sql += "SELECT 'Feature' As type, ST_AsGeoJSON(lgeom.pretensao)::json As geometry, row_to_json(lprop) As properties ";
    sql += 'FROM plantas.pedidodetail As lgeom ';
    sql += 'INNER JOIN ( ';
    sql += 'SELECT id, gid FROM plantas.pedidodetail ';
    sql += 'where gid = ' + gid;
    sql += ' ) As lprop ';
    sql += 'ON lgeom.gid = lprop.gid ';
    //-- fim de query
    sql += ') As f )  As fc';

To write the features back to the database, I use the writeFeatures like:

            var features = printrequestdetaillayer.getSource().getFeatures();

            if (features.length) {
                var details = (new ol.format.GeoJSON()).writeFeatures(features);
                Server.Plantas.Pedidos.saveGeoJsonDetail({
                    pedido: result.data[0].gid,
                    features: details
                }, function (result, event) {
                    if (result.success) {
                        console.log('Ok', result.message);
                    } else {
                        console.log('Not ok', result.message);
                    }
                });
            }
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.