From d7655be7dcb734167e4285092343f316bd40565f Mon Sep 17 00:00:00 2001 From: mpeltriaux Date: Fri, 7 Oct 2022 13:56:43 +0200 Subject: [PATCH] Map client update * updates map client to most recent code * fixes bug on as_feature_collection which led to problems on initial loading in map --- konova/models/geometry.py | 11 +++- .../map/client/libs/netgis/MapOpenLayers.js | 62 ++++++++----------- 2 files changed, 36 insertions(+), 37 deletions(-) diff --git a/konova/models/geometry.py b/konova/models/geometry.py index 6e7b0b6f..f69fcc7e 100644 --- a/konova/models/geometry.py +++ b/konova/models/geometry.py @@ -221,8 +221,17 @@ class Geometry(BaseResource): polygons.append(p) geojson = { "type": "FeatureCollection", + "crs": { + "type": "name", + "properties": { + "name": f"urn:ogc:def:crs:EPSG::{geom.srid}" + } + }, "features": [ - json.loads(x.geojson) for x in polygons + { + "type": "Feature", + "geometry": json.loads(x.geojson) + } for x in polygons ] } return geojson diff --git a/templates/map/client/libs/netgis/MapOpenLayers.js b/templates/map/client/libs/netgis/MapOpenLayers.js index 640b39ec..5321cce5 100644 --- a/templates/map/client/libs/netgis/MapOpenLayers.js +++ b/templates/map/client/libs/netgis/MapOpenLayers.js @@ -394,16 +394,12 @@ netgis.MapOpenLayers.prototype.clearAll = function() { for ( var i = 0; i < this.layers.length; i++ ) { - if(this.layers[i] === this.editLayer){ - continue; - } this.map.removeLayer( this.layers[ i ] ); } - this.layers = [this.editLayer]; + this.layers = []; this.snapFeatures.clear(); - this.snapFeatures.push(this.editLayer); }; netgis.MapOpenLayers.prototype.onUpdateStyle = function( e ) @@ -1144,15 +1140,9 @@ netgis.MapOpenLayers.prototype.updateEditLayerItem = function() netgis.MapOpenLayers.prototype.onEditFeaturesLoaded = function( e ) { - var json = e; - var format = new ol.format.GeoJSON(); - var features = format.readFeatures( json ); - - this.editLayer.getSource().addFeatures( features ); - //this.snapFeatures.push( e.feature ); - - if ( features.length > 0 ) - this.view.fit( this.editLayer.getSource().getExtent(), { padding: [ 40, 40, 40, 40 ] } ); + var json = e; + var self = this; + window.setTimeout( function() { self.createLayerGeoJSON( "Import", json ); }, 10 ); }; netgis.MapOpenLayers.prototype.onDragEnter = function( e ) @@ -1267,6 +1257,7 @@ netgis.MapOpenLayers.prototype.createLayerGeoJSON = function( title, data ) //NOTE: netgis.util.foreach( proj4.defs, function( k,v ) { console.info( "DEF:", k, v ); } ) var projcode = projection.getCode(); + switch ( projcode ) { case "EPSG:3857": @@ -1277,14 +1268,14 @@ netgis.MapOpenLayers.prototype.createLayerGeoJSON = function( title, data ) //console.info( "Import Projection:", projcode ); break; } - + default: { // Projection Not Supported console.warn( "Unsupported Import Projection:", projcode ); break; } - } + } this.addImportedFeatures( features ); }; @@ -1304,57 +1295,57 @@ netgis.MapOpenLayers.prototype.createLayerGML = function( title, data ) //var format = new ol.format.WFS( { featureNS: "ogr", featureType: "RLP_OG_utf8_epsg4326" } ); //var projection = format.readProjection( data ); //var features = format.readFeatures( data, { dataProjection: "EPSG:4326", featureProjection: "EPSG:3857" } ); - + //var features = format.readFeatures( data, { dataProjection: this.client.config.map.projection, featureProjection: this.client.config.map.projection } ); //console.info( "GML:", projection, features, features[ 0 ].getGeometry() ); var features = []; - + var parser = new DOMParser(); var xml = parser.parseFromString( data, "text/xml" ); - + // Features var featureMembers = xml.getElementsByTagName( "gml:featureMember" ); - + for ( var f = 0; f < featureMembers.length; f++ ) { var props = {}; - + var node = featureMembers[ f ]; var child = node.children[ 0 ]; - + // Attributes for ( var a = 0; a < child.attributes.length; a++ ) { var attribute = child.attributes[ a ]; props[ attribute.nodeName ] = attribute.nodeValue; } - + for ( var c = 0; c < child.children.length; c++ ) { var childNode = child.children[ c ]; - + if ( childNode.nodeName === "ogr:geometryProperty" ) continue; - + var parts = childNode.nodeName.split( ":" ); var k = parts[ parts.length - 1 ]; var v = childNode.innerHTML; - + props[ k ] = v; } - + // Geometry var geomprop = child.getElementsByTagName( "ogr:geometryProperty" )[ 0 ]; - + //for ( var g = 0; g < geomprop.children.length; g++ ) { var geom = geomprop.children[ 0 ]; var proj = geom.getAttribute( "srsName" ); - + if ( proj && proj !== "EPSG:4326" && proj !== this.client.config.map.projection ) console.warn( "Unsupported Import Projection:", proj ); - + switch ( geom.nodeName ) { case "gml:Polygon": @@ -1362,7 +1353,7 @@ netgis.MapOpenLayers.prototype.createLayerGML = function( title, data ) props[ "geometry" ] = this.gmlParsePolygon( geom, proj ); break; } - + case "gml:MultiPolygon": { props[ "geometry" ] = this.gmlParseMultiPolygon( geom, proj ); @@ -1370,7 +1361,7 @@ netgis.MapOpenLayers.prototype.createLayerGML = function( title, data ) } } } - + var feature = new ol.Feature( props ); features.push( feature ); } @@ -1452,23 +1443,22 @@ netgis.MapOpenLayers.prototype.createLayerShapefile = function( title, shapeData netgis.MapOpenLayers.prototype.addImportedFeatures = function( features ) { - // ToDO: Changes in here problematic on initial data loading // Add To Edit Layer this.editEventsSilent = true; this.editLayer.getSource().addFeatures( features ); this.editEventsSilent = false; this.updateEditOutput(); - + // Zoom Imported Features if ( features.length > 0 ) { var extent = features[ 0 ].getGeometry().getExtent(); - + for ( var f = 1; f < features.length; f++ ) { ol.extent.extend( extent, features[ f ].getGeometry().getExtent() ); } - + var padding = 40; this.view.fit( extent, { duration: 300, padding: [ padding, padding, padding, padding ] } ); }