From 8192a5d5d9024cb6b0dbaf9b50ff35c8b691a668 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB?= Date: Fri, 15 Mar 2024 12:36:49 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A6=D0=B5=D0=BD=D1=82=D1=80=D0=BE=D0=B2?= =?UTF-8?q?=D0=BA=D0=B0=20=D0=BA=D0=B0=D1=80=D1=82=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- explication.js | 195 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 192 insertions(+), 3 deletions(-) diff --git a/explication.js b/explication.js index f55831a..0467f29 100644 --- a/explication.js +++ b/explication.js @@ -91,10 +91,11 @@ L.OSM.park_explication = function(osm_obj_type, osm_obj_id, f_fin_ok){ }; L.OSM.park_explication.prototype.map = function (div, map_prov, map_params) { - var cen = this.geoJsonGeneral.features[0].geometry.coordinates[0][0]; + var mrg = this.general_rel_geoJson(this.geoJsonGeneral); + var cen = turf_centroid(mrg); var md = new mapDiv( div, - [cen[1], cen[0]], + cen, map_prov.tileLayers, map_prov.Names, { @@ -105,7 +106,7 @@ L.OSM.park_explication = function(osm_obj_type, osm_obj_id, f_fin_ok){ true, map_params ); - var mrg = this.general_rel_geoJson(this.geoJsonGeneral); + var n = mrg.properties.tags.name; var mr = L.geoJSON(mrg, { fillOpacity: 0, color: "#F2872F" }); md.map.fitBounds(mr.getBounds()); @@ -635,3 +636,191 @@ L.OSM.park_explication.prototype.привязка_указателей = functio o.webData.Тип_информации = '' + п + ''; } }; + +function turf_centroid(geojson) { + var xSum = 0; + var ySum = 0; + var len = 0; + turf_coordEach(geojson, function (coord) { + xSum += coord[0]; + ySum += coord[1]; + len++; + }, true); + return [ySum / len, xSum / len]; +} + +/** + * Iterate over coordinates in any GeoJSON object, similar to Array.forEach() + * + * @name coordEach + * @param {FeatureCollection|Feature|Geometry} geojson any GeoJSON object + * @param {Function} callback a method that takes (currentCoord, coordIndex, featureIndex, multiFeatureIndex) + * @param {boolean} [excludeWrapCoord=false] whether or not to include the final coordinate of LinearRings that wraps the ring in its iteration. + * @returns {void} + * @example + * var features = turf.featureCollection([ + * turf.point([26, 37], {"foo": "bar"}), + * turf.point([36, 53], {"hello": "world"}) + * ]); + * + * turf.coordEach(features, function (currentCoord, coordIndex, featureIndex, multiFeatureIndex, geometryIndex) { + * //=currentCoord + * //=coordIndex + * //=featureIndex + * //=multiFeatureIndex + * //=geometryIndex + * }); + */ +function turf_coordEach(geojson, callback, excludeWrapCoord) { + // Handles null Geometry -- Skips this GeoJSON + if (geojson === null) return; + var j, + k, + l, + geometry, + stopG, + coords, + geometryMaybeCollection, + wrapShrink = 0, + coordIndex = 0, + isGeometryCollection, + type = geojson.type, + isFeatureCollection = type === "FeatureCollection", + isFeature = type === "Feature", + stop = isFeatureCollection ? geojson.features.length : 1; + + // This logic may look a little weird. The reason why it is that way + // is because it's trying to be fast. GeoJSON supports multiple kinds + // of objects at its root: FeatureCollection, Features, Geometries. + // This function has the responsibility of handling all of them, and that + // means that some of the `for` loops you see below actually just don't apply + // to certain inputs. For instance, if you give this just a + // Point geometry, then both loops are short-circuited and all we do + // is gradually rename the input until it's called 'geometry'. + // + // This also aims to allocate as few resources as possible: just a + // few numbers and booleans, rather than any temporary arrays as would + // be required with the normalization approach. + for (var featureIndex = 0; featureIndex < stop; featureIndex++) { + geometryMaybeCollection = isFeatureCollection + ? geojson.features[featureIndex].geometry + : isFeature + ? geojson.geometry + : geojson; + isGeometryCollection = geometryMaybeCollection + ? geometryMaybeCollection.type === "GeometryCollection" + : false; + stopG = isGeometryCollection + ? geometryMaybeCollection.geometries.length + : 1; + + for (var geomIndex = 0; geomIndex < stopG; geomIndex++) { + var multiFeatureIndex = 0; + var geometryIndex = 0; + geometry = isGeometryCollection + ? geometryMaybeCollection.geometries[geomIndex] + : geometryMaybeCollection; + + // Handles null Geometry -- Skips this geometry + if (geometry === null) continue; + coords = geometry.coordinates; + var geomType = geometry.type; + + wrapShrink = + excludeWrapCoord && + (geomType === "Polygon" || geomType === "MultiPolygon") + ? 1 + : 0; + + switch (geomType) { + case null: + break; + case "Point": + if ( + callback( + coords, + coordIndex, + featureIndex, + multiFeatureIndex, + geometryIndex + ) === false + ) + return false; + coordIndex++; + multiFeatureIndex++; + break; + case "LineString": + case "MultiPoint": + for (j = 0; j < coords.length; j++) { + if ( + callback( + coords[j], + coordIndex, + featureIndex, + multiFeatureIndex, + geometryIndex + ) === false + ) + return false; + coordIndex++; + if (geomType === "MultiPoint") multiFeatureIndex++; + } + if (geomType === "LineString") multiFeatureIndex++; + break; + case "Polygon": + case "MultiLineString": + for (j = 0; j < coords.length; j++) { + for (k = 0; k < coords[j].length - wrapShrink; k++) { + if ( + callback( + coords[j][k], + coordIndex, + featureIndex, + multiFeatureIndex, + geometryIndex + ) === false + ) + return false; + coordIndex++; + } + if (geomType === "MultiLineString") multiFeatureIndex++; + if (geomType === "Polygon") geometryIndex++; + } + if (geomType === "Polygon") multiFeatureIndex++; + break; + case "MultiPolygon": + for (j = 0; j < coords.length; j++) { + geometryIndex = 0; + for (k = 0; k < coords[j].length; k++) { + for (l = 0; l < coords[j][k].length - wrapShrink; l++) { + if ( + callback( + coords[j][k][l], + coordIndex, + featureIndex, + multiFeatureIndex, + geometryIndex + ) === false + ) + return false; + coordIndex++; + } + geometryIndex++; + } + multiFeatureIndex++; + } + break; + case "GeometryCollection": + for (j = 0; j < geometry.geometries.length; j++) + if ( + coordEach(geometry.geometries[j], callback, excludeWrapCoord) === + false + ) + return false; + break; + default: + throw new Error("Unknown Geometry Type"); + } + } + } +}