سؤال

I was curious ab your opinions whats the most efficient transfer format between SpatiaLite and OpenLayers. Currently Im developing an application based on SpatiaLite (extension of SQLite) and OpenLayers and as a transfer format I use GeoJSON.

My procedure: 1) quering DB by php script, using SpatiaLite's function AsGeoJSON, thus obtaining geojson formatted data

2) using php's print() to transfer retrieved data from php variable to JS variable:

$result = $db->query($query);

$r = '{"type": "FeatureCollection","features": [';
while($row = $result->fetchArray(SQLITE3_ASSOC))
{
  $r .= '{"type":"Feature","properties":{}, "geometry":' . $row['geometry'] . '},';
}
$r .= ']}';
print'<script> CadastreBorder = ' . $r . '; </script>';

3) creating the features for Vector Layer in OpenLayers by reading

var vectorLayer = new OpenLayers.Layer.Vector(name, {style: style, rendererOptions:
{zIndexing: true}});
var formatGeoJSON = new OpenLayers.Format.GeoJSON({});
vectorLayer.addFeatures(formatGeoJSON.read(CadastreBorder));
map.addLayer(vectorLayer);

Is there any way how to achieve the same goal more efficiently and more nicely? Thanks!

هل كانت مفيدة؟

المحلول

Not sure about speed, but if you're using recent version of PHP (5.2 >=) you'd might consider using json_encode on php's arrays. Instead of creating string you'd create an array looking somewhere like this:

$geoJSON_array = Array(
    "type" => "FeatureCollection",
    "features" => Array()
    );

And for each row of geometry, add new table to "features":

Array(
    "type" => "Feature",
    "properties" => Array(),
    "geometry" => Array(... your geometry),
)

After creating such array, run json_encode over it and you're home. For safety I'd check it validates (e.g. using http://geojsonlint.com/). This may help you later on, when e.g. there would be need to support new information, or to add new kinds of elements to the map.

Question: Why are you not using fixed strategy for loading points? If you're creating this geoJSON to file/as script, you may also tell openlayers to download it automatically, likes this:

var vectorLayer = new OpenLayers.Layer.Vector(name, {
    strategies: [new OpenLayers.Strategy.Fixed()],
    protocol: new OpenLayers.Protocol.HTTP({
        url: "json/my_geojson.json",
        format: new OpenLayers.Format.GeoJSON()
    }),
    style: style,
    rendererOptions: {zIndexing: true}
});
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top