openlayers / ol2

OpenLayers v2 - deprecated!
Other
1.48k stars 771 forks source link

Some WKT strings not working when loaded through Strategy.Fixed #1355

Open firstStraw opened 10 years ago

firstStraw commented 10 years ago

If I load a single WKT MULTIPOLYGON through a direct call to Format.WKT.read, it works fine. If I load the exact same string using Protocol.HTTP and Strategy.Fixed, it doesn't work. If I wrap the MULTIPOLYGON in a GEOMETRYCOLLECTION, then it can be loaded through HTTP and Fixed.

In the Strategy.Fixed.merge function, I removed the && features.length > 0 part of the first "if" check, and this made my tests work fine. It appears that GEOMETRYCOLLECTIONs are represented differently than MULTIPOLYGONs in the return from Format.WKT.read. Through this change made my tests work, I don't know whether there was a good reason the "if" check was written the way it was or what the implications elsewhere might be.

Here is a code snippet. The "multipolygon" file contains the same string defined in the JavaScript as "wktStr". The "geometrycollection" file contains that same string but prepended with "GEOMETRYCOLLECTION (" and appended with ")".

<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="content-type" content="text/html;charset=utf-8" />
  <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?v=3.exp&libraries=drawing&sensor=false"></script>
  <script type="text/javascript" src="http://openlayers.org/api/OpenLayers.js"></script>
  <!--<script type="text/javascript" src="OpenLayers.debug.js"></script>-->
  <style type="text/css">
    #map {
      width: 600px;
      height: 450px;
      border: 1px solid black;
    }
  </style>
  <script type="text/javascript">
    function init() {
      var map = new OpenLayers.Map('map');
      var baseLayer = new OpenLayers.Layer.WMS('OpenLayers WMS', 'http://vmap0.tiles.osgeo.org/wms/vmap', { layers: 'basic' });
      map.addLayer(baseLayer);
      map.setCenter(new OpenLayers.LonLat(-120, 31.75), 5);
      map.addControl(new OpenLayers.Control.LayerSwitcher());

      var format = new OpenLayers.Format.WKT();

      // This produces a couple of big orange polygons on the map
      var wktStr = "MULTIPOLYGON (((-120 35, -105 35, -105 23, -120 23, -120 35)), ((-144 36, -127 36, -127 23, -144 23, -144 36)))";
      var features = format.read(wktStr);
      var wktLayer1 = new OpenLayers.Layer.Vector('Multipolygon from string variable');
      wktLayer1.addFeatures(features);
      wktLayer1.setVisibility(false);
      map.addLayer(wktLayer1);

      // This should produce a couple of big orange polygons on the map, but doesn't
      var protocol2 = new OpenLayers.Protocol.HTTP({ url: 'multipolygon', format: format });
      var wktLayer2 = new OpenLayers.Layer.Vector('Multipolygon from HTTP', {
        protocol: protocol2,
        strategies: [ new OpenLayers.Strategy.Fixed() ]
      });
      wktLayer2.setVisibility(true);
      map.addLayer(wktLayer2);

      // This produces a couple of big orange polygons on the map.  This the
      // same as the multipolygon file but the MULTIPOLYGON is wrapped in a
      // GEOMETRYCOLLECTION.
      var protocol3 = new OpenLayers.Protocol.HTTP({ url: 'geometrycollection', format: format });
      var wktLayer3 = new OpenLayers.Layer.Vector('Geometry collection from HTTP', {
        protocol: protocol3,
        strategies: [ new OpenLayers.Strategy.Fixed() ]
      });
      wktLayer3.setVisibility(false);
      map.addLayer(wktLayer3);
    }
  </script>
</head>
<body onload="init()">
<div id="map"></div>
</body>
</html>
aaronpmishkin commented 8 years ago

I am experiencing a similar problem. Any potential fix for this?