Ticket #2024 (closed feature: fixed)

Opened 4 years ago

Last modified 4 years ago

GeoJSON + OpenStreetMap Projection problems for FeatureCollections

Reported by: ibolmo Owned by: crschmidt
Priority: critical Milestone: 2.8 Release
Component: Format.GeoJSON Version: 2.7
Keywords: Cc:
State: Complete

Description

Took a while to weed this bug out, but the problem is on line 223 of OpenLayers/GeoJSON.js

                components[i] = this.parseGeometry.apply(
                    this, [obj.geometries[i]]
                );

Coupled with lines 244-247:

        if (this.internalProjection && this.externalProjection) {
            geometry.transform(this.externalProjection, 
                               this.internalProjection); 
        }

Basically,for GeometryCollections, each geometry is parsed with parseGeometry and then transformed. Eventually, the loop will end and the components are instantiated in a Geometry.Collection.

Because Geometry.Collections have their own transform method, each component will be transformed once again. This is the bug.

The solution is to return on line 227:

            return new OpenLayers.Geometry.Collection(components);

So that the geometry collection is not (re)transformed.

Test Case:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
	<head>
        <style type="text/css" media="screen">
            #map { height: 500px; }
        </style>
        <script src="openlayers.js" type="text/javascript" charset="utf-8"></script>
        <script src="http://www.openstreetmap.org/openlayers/OpenStreetMap.js" type="text/javascript" charset="utf-8"></script>
        <script type="text/javascript" charset="utf-8">
            function init(){
                var map = new OpenLayers.Map ("map", {
                    controls: [
                        new OpenLayers.Control.Navigation(),
                        new OpenLayers.Control.Attribution()
                    ],
                    maxExtent: new OpenLayers.Bounds(-20037508.34,-20037508.34,20037508.34,20037508.34),
                    maxResolution: 156543.0399,
                    numZoomLevels: 19,
                    units: 'm',
                    projection: new OpenLayers.Projection("EPSG:900913"),
                    displayProjection: new OpenLayers.Projection("EPSG:4326")
                });
                
                var osm = new OpenLayers.Layer.OSM.Mapnik('OSM');
                map.addLayer(osm);
                var lonLat = new OpenLayers.LonLat(5, 40).transform(new OpenLayers.Projection("EPSG:4326"), map.getProjectionObject());
                map.setCenter (lonLat, 5);
                
                var featurecollection = {
                    "type": "FeatureCollection", 
                    "features": [{
                        "geometry": {
                            "type": "GeometryCollection", 
                            "geometries": [
                                {
                                    "type": "LineString", 
                                    "coordinates": 
                                        [[11.0878902207, 45.1602390564], 
                                        [15.01953125, 48.1298828125]]
                                },
                                {
                                    "type": "Polygon", 
                                    "coordinates": 
                                        [[[11.0878902207, 45.1602390564], 
                                          [14.931640625, 40.9228515625], 
                                          [0.8251953125, 41.0986328125], 
                                          [7.63671875, 48.96484375], 
                                          [11.0878902207, 45.1602390564]]]
                                },
                                {
                                    "type":"Point", 
                                    "coordinates":[15.87646484375, 44.1748046875]                                    
                                }
                            ]
                        }, 
                        "type": "Feature", 
                        "properties": {}    
                    }]
                };
                var geojson_format = new OpenLayers.Format.GeoJSON({
                    'internalProjection': new OpenLayers.Projection("EPSG:900913"),
                    'externalProjection': new OpenLayers.Projection("EPSG:4326")
                });
                var vector_layer = new OpenLayers.Layer.Vector(); 
                map.addLayer(vector_layer);
                vector_layer.addFeatures(geojson_format.read(featurecollection));
            };
        </script>
	</head>
	<body onload="init()">
	    <div id="map"></div>
	</body>
</html>

Attachments

GeoJSON.patch Download (0.5 KB) - added by ibolmo 4 years ago.

Change History

Changed 4 years ago by ibolmo

Changed 4 years ago by crschmidt

  • owner changed from tschaub to crschmidt
  • status changed from new to assigned
  • milestone changed from 2.9 Release to 2.8 Release

Changed 4 years ago by crschmidt

  • state set to Review

Changed 4 years ago by crschmidt

  • keywords projection featurecollection removed
  • status changed from assigned to closed
  • state changed from Review to Complete
  • resolution set to fixed

(In [9181]) Fix for GeoJSON + Reprojection problems for GeometryCollections. Original patch/ diagnosis by ibolmo, refactored by me. Includes manual test. (Closes #2024)

Note: See TracTickets for help on using tickets.