Skip to content

Commit

Permalink
Merge pull request #2 from OSM-Catalan/master
Browse files Browse the repository at this point in the history
Habilitació càrrega dades GEOJSON
  • Loading branch information
Ripollx authored May 9, 2021
2 parents d4c69fc + 0b4a2a9 commit b887ff3
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 61 deletions.
15 changes: 15 additions & 0 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -1036,6 +1036,21 @@ var config = {
group: 'Test',
title: 'node[name=]',
query: 'node[name=]({{bbox}});out meta;'
},
{
group: 'Test',
title: 'Stops signs at geojson',
geojson: 'https://raw.githubusercontent.com/yopaseopor/osmhistoricmap/master/src/img/base/test.geojson',
iconSrc: 'https://raw.githubusercontent.com/yopaseopor/beta_preset_josm/master/ES/traffic_signs/ES/ES_R2.png',
style: function () {
var style = new ol.style.Style({
image: new ol.style.Icon({
scale: 0.4,
src: 'https://raw.githubusercontent.com/yopaseopor/beta_preset_josm/master/ES/traffic_signs/ES/ES_R2.png'
})
});
return style;
}
},
{
group: 'Test',
Expand Down
131 changes: 70 additions & 61 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,68 +29,77 @@ $(function () {
var layerGroup = overlay['group'],
vectorProperties = overlay,
vector;


if (overlay['geojson'] !== undefined) {
var vectorSource = new ol.source.Vector({
format: new ol.format.GeoJSON(),
url: overlay['geojson']
})
} else {
var vectorSource = new ol.source.Vector({
format: new ol.format.OSMXML2(),
loader: function (extent, resolution, projection) {
loading.show();
var me = this;
var epsg4326Extent = ol.proj.transformExtent(extent, projection, 'EPSG:4326');
var query = '[maxsize:536870912];' + overlay['query']; // Memory limit 512 MiB
//var query = layerQuery;
query = query.replace(/{{bbox}}/g, epsg4326Extent[1] + ',' + epsg4326Extent[0] + ',' + epsg4326Extent[3] + ',' + epsg4326Extent[2]);

var client = new XMLHttpRequest();
client.open('POST', config.overpassApi());
client.onloadend = function () {
loading.hide();
};
client.onerror = function () {
console.error('[' + client.status + '] Error loading data.');
me.removeLoadedExtent(extent);
vector.setVisible(false);
};
client.onload = function () {
if (client.status === 200) {
var xmlDoc = $.parseXML(client.responseText),
xml = $(xmlDoc),
remark = xml.find('remark'),
nodosLength = xml.find('node').length;

if (remark.length !== 0) {
console.error('Error:', remark.text());
$('<div>').html(remark.text()).dialog({
modal: true,
title: 'Error',
close: function () {
$(this).dialog('destroy');
}
});
client.onerror.call(this);
} else {
console.log('Nodes Found:', nodosLength);
if (nodosLength === 0) {
$('<div>').html(config.i18n.noNodesFound).dialog({
modal: true,
//title: 'Error',
close: function () {
$(this).dialog('destroy');
}
});
}
var features = new ol.format.OSMXML2().readFeatures(xmlDoc, {
featureProjection: map.getView().getProjection()
});
me.addFeatures(features);
}
} else {
client.onerror.call(this);
}
};
client.send(query);
},
strategy: ol.loadingstrategy.bbox
});
}

var vectorSource = new ol.source.Vector({
format: new ol.format.OSMXML2(),
loader: function (extent, resolution, projection) {
loading.show();
var me = this;
var epsg4326Extent = ol.proj.transformExtent(extent, projection, 'EPSG:4326');
var query = '[maxsize:536870912];' + overlay['query']; // Memory limit 512 MiB
//var query = layerQuery;
query = query.replace(/{{bbox}}/g, epsg4326Extent[1] + ',' + epsg4326Extent[0] + ',' + epsg4326Extent[3] + ',' + epsg4326Extent[2]);

var client = new XMLHttpRequest();
client.open('POST', config.overpassApi());
client.onloadend = function () {
loading.hide();
};
client.onerror = function () {
console.error('[' + client.status + '] Error loading data.');
me.removeLoadedExtent(extent);
vector.setVisible(false);
};
client.onload = function () {
if (client.status === 200) {
var xmlDoc = $.parseXML(client.responseText),
xml = $(xmlDoc),
remark = xml.find('remark'),
nodosLength = xml.find('node').length;

if (remark.length !== 0) {
console.error('Error:', remark.text());
$('<div>').html(remark.text()).dialog({
modal: true,
title: 'Error',
close: function () {
$(this).dialog('destroy');
}
});
client.onerror.call(this);
} else {
console.log('Nodes Found:', nodosLength);
if (nodosLength === 0) {
$('<div>').html(config.i18n.noNodesFound).dialog({
modal: true,
//title: 'Error',
close: function () {
$(this).dialog('destroy');
}
});
}
var features = new ol.format.OSMXML2().readFeatures(xmlDoc, {
featureProjection: map.getView().getProjection()
});
me.addFeatures(features);
}
} else {
client.onerror.call(this);
}
};
client.send(query);
},
strategy: ol.loadingstrategy.bbox
});

vectorProperties['source'] = vectorSource;
vectorProperties['visible'] = false;
Expand Down

0 comments on commit b887ff3

Please sign in to comment.