Project

General

Profile

« Previous | Next » 

Revision 8720969e

Added by Andreas Kohlbecker almost 7 years ago

ref #6608 increasing consistence of projection and bbox handling

View differences:

modules/cdm_dataportal/js/map/openlayers_map.js
95 95
        };
96 96
        var mapExtends = {
97 97
                epsg_4326: new OpenLayers.Bounds(-180, -90, 180, 90),
98
                epsg_900913: new OpenLayers.Bounds(-180, -90, 180, 90),
99
                epsg_3857: new OpenLayers.Bounds(-180, -90, 180, 90)
98
                //  Spherical Mercator epsg_900913 is not suppporting the whole marble
99
                epsg_900913: new OpenLayers.Bounds(-179, -85, 179, 85),
100
                //  Spherical Mercator
101
                epsg_3857: new OpenLayers.Bounds(-179, -85, 179, 85)
100 102
        };
101 103
        // transform epsg_900913 to units meter
102 104
        mapExtends.epsg_900913.transform(projections.epsg_4326, projections.epsg_900913);
......
222 224
              tdwg4: 'topp:tdwg_level_4'
223 225
      };
224 226

  
225
      if(opts.resizable == true) {
227
      if(opts.resizable === true) {
226 228
        // resizable requires jQueryUI to  be loaded!!!
227 229
        mapContainerElement.resizable({
228 230
          resize: function( event, ui ) {
......
384 386
          log(" > zoomToExtend done", true);
385 387
        };
386 388

  
389
      /**
390
       * Returns  the projection of the defaultBaseLayer which is the
391
       * the projection to which all other layers and locations must be transformed.
392
       */
393
      var referenceProjection = function() {
394
        if(defaultBaseLayer){
395
          return defaultBaseLayer.projection;
396
        } else {
397
          log("Error - referenceProjection() defaultBaseLayer not set");
398
          return null;
399
        }
400
      };
401

  
402
      /**
403
       * Returns the maxExtent of the defaultBaseLayer.
404
       */
405
      var referenceMaxExtent = function() {
406
        if(defaultBaseLayer){
407
          return defaultBaseLayer.maxExtent;
408
        } else {
409
          log("Error - referenceMaxExtent() defaultBaseLayer not set");
410
          return null;
411
        }
412
      };
413

  
387 414
        var getHeight = function(){
388 415
          return mapContainerElement.width() / opts.aspectRatio;
389 416
        };
......
447 474
                  info += "<dt>baselayer projection:<dt><dd>" + map.baseLayer.projection.getCode() + "</dd>";
448 475
                }
449 476
            } else {
450
                info += "<dt>bbox:<dt><dd>" + (mapExtendDegree != null ? mapExtendDegree.toBBOX() : 'NULL') + "</dd>";
477
                info += "<dt>bbox:<dt><dd>" + (mapExtendDegree !== null ? mapExtendDegree.toBBOX() : 'NULL') + "</dd>";
451 478
            }
452 479
            info += "</dl>";
453 480

  
454
            if(infoElement == null){
481
            if(infoElement === null){
455 482
                infoElement = jQuery('<div class="map_info"></div>');
456 483
                mapElement.parent().after(infoElement);
457 484
            }
......
472 499
//          var maxExtentByAspectRatio = cropBoundsToAspectRatio(defaultBaseLayer.maxExtent, getWidth/getHeight);
473 500
          var maxResolution = null;
474 501
          // gmaps has no maxExtent at this point, need to check for null
475
          if(defaultBaseLayer.maxExtent != null){
502
          if(referenceMaxExtent() !== null){
476 503
              maxResolution = Math[(opts.displayOutsideMaxExtent ? 'max' : 'min')](
477
                      defaultBaseLayer.maxExtent.getWidth() / getWidth(),
478
                      defaultBaseLayer.maxExtent.getHeight() / getHeight()
504
                referenceMaxExtent().getWidth() / getWidth(),
505
                referenceMaxExtent().getHeight() / getHeight()
479 506
              );
480 507
          }
481 508
          console.log("mapOptions.maxResolution: " + maxResolution);
482
          console.log("mapOptions.restrictedExtent: " + defaultBaseLayer.maxExtent);
509
          console.log("mapOptions.restrictedExtent: " + referenceMaxExtent());
483 510

  
484 511
          map = new OpenLayers.Map(
485 512
            mapElement.attr('id'),
......
498 525

  
499 526
              // setting restrictedExtent the the maxExtent prevents from panning the
500 527
              // map out of its bounds
501
              restrictedExtent: defaultBaseLayer.maxExtent,
502
//                      maxExtent: defaultBaseLayer.maxExtent,
528
              restrictedExtent: referenceMaxExtent(),
529
//                      maxExtent: referenceMaxExtent(),
503 530

  
504 531
              // Setting the map.fractionalZoom property to true allows zooming to an arbitrary level
505 532
              // (between the min and max resolutions).
......
521 548
          map.setBaseLayer(defaultBaseLayer);
522 549

  
523 550
          // calculate the bounds to zoom to
524
          zoomToBounds = zoomToBoundsFor(opts.boundingBox ? opts.boundingBox : defaultBaseLayerBoundingBox, defaultBaseLayer);
525
          zoomToBounds = cropBoundsToAspectRatio(zoomToBounds, map.getSize().w / map.getSize().h);
551
          zoomToBounds = calculateZoomToBounds(opts.boundingBox ? opts.boundingBox : defaultBaseLayerBoundingBox);
552
          // zoomToBounds = cropBoundsToAspectRatio(zoomToBounds, map.getSize().w / map.getSize().h);
526 553
          console.log("baselayer zoomToBounds: " + zoomToBounds);
527 554

  
528 555
        };
......
779 806

  
780 807
        /**
781 808
         *
782
         * @param OpenLayers.Bounds b
783
         * @param float aspectRatio width/height
809
         * @param b OpenLayers.Bounds to crop
810
         * @param aspectRatio as fraction of width/height as float value
784 811
         *
785 812
         * @return the bounds cropped to the given aspectRatio
786 813
         */
......
788 815

  
789 816
            var cropedB = b.clone();
790 817

  
791
            if(aspectRatio == 1){
818
            if(aspectRatio === 1){
792 819
                return cropedB;
793 820
            }
794 821

  
795 822
            /*
796 823
             * LonLat:
797
             *   lon {Float} The x-axis coodinate in map units
824
             *   lon {Float} The x-axis coordinate in map units
798 825
             *   lat {Float} The y-axis coordinate in map units
799 826
             */
800 827
            var center = cropedB.getCenterLonLat();
828
            var dist;
801 829
            if(aspectRatio < 1){
802
                var dist = (b.getHeight() / 2) * aspectRatio;
830
                dist = (b.getHeight() / 2) * aspectRatio;
803 831
                cropedB.top = center.lat + dist;
804 832
                cropedB.cropedBottom = center.lat - dist;
805 833
            } else if(aspectRatio > 1){
806
                var dist = (b.getWidth() / 2) / aspectRatio;
834
                dist = (b.getWidth() / 2) / aspectRatio;
807 835
                cropedB.left = center.lon - dist;
808 836
                cropedB.right = center.lon + dist;
809 837
            }
......
830 858
      /**
831 859
       * returns the zoom to bounds.
832 860
       *
833
       * NOTE: only used for the base layer
834
       *
835 861
       * @param bboxString
836 862
       *     a string representation of the bounds in degree for epsg_4326
837
       * @param layer
838
       *     the Openlayers.Layer
839 863
       *
840 864
       * @return the bboxstring projected onto the layer and intersected with the maximum extent of the layer
841 865
       */
842
      var zoomToBoundsFor = function(bboxString, layer){
866
      var calculateZoomToBounds = function(bboxString){
843 867
        var zoomToBounds;
844 868
        if(bboxString) {
845 869
          zoomToBounds = OpenLayers.Bounds.fromString(bboxString);
870
          if(referenceProjection().proj.projName){
871
            // SpericalMercator is not supporting the full extent -180,-90,180,90
872
            // crop if need to -179, -85, 179, 85
873
            if(zoomToBounds.left < -179){
874
              zoomToBounds.left =  -179;
875
            }
876
            if(zoomToBounds.bottom < -85){
877
              zoomToBounds.bottom =  -85;
878
            }
879
            if(zoomToBounds.right > 179){
880
              zoomToBounds.right =  179;
881
            }
882
            if(zoomToBounds.top > 85){
883
              zoomToBounds.top = 85;
884
            }
885
          }
846 886
          // transform bounding box given in degree values to the projection of the base layer
847
          zoomToBounds.transform(CdmOpenLayers.projections.epsg_4326, layer.projection);
848
        } else if(layer.maxExtent) {
849
          zoomToBounds = layer.maxExtent;
887
          zoomToBounds.transform(CdmOpenLayers.projections.epsg_4326, referenceProjection());
888
        } else if(referenceMaxExtent()) {
889
          zoomToBounds = referenceMaxExtent();
850 890
          // no need to transform since the bounds are obtained from the layer
851 891
        } else {
852
          zoomToBounds = new OpenLayers.Bounds(-180, -90, 180, 90);
892
          // use the more narrow bbox of the SphericalMercator to avoid reprojection problems
893
          // SpericalMercator is not supporting the full extent!
894
          zoomToBounds = CdmOpenLayers.mapExtends.epsg_900913;
853 895
          // transform bounding box given in degree values to the projection of the base layer
854
          zoomToBounds.transform(CdmOpenLayers.projections.epsg_4326, layer.projection);
896
          zoomToBounds.transform(CdmOpenLayers.projections.epsg_4326, referenceProjection());
855 897
        }
856 898

  
857
        zoomToBounds = intersectionOfBounds(layer.maxExtent, zoomToBounds);
899
        zoomToBounds = intersectionOfBounds(referenceMaxExtent(), zoomToBounds);
900

  
901
        log("zoomBounds calculated: " + zoomToBounds.toString());
858 902

  
859 903
        return zoomToBounds;
860 904
      };
861 905

  
862 906
      var log = function(message, addTimeStamp){
863 907
        var timestamp = '';
864
        if(addTimeStamp == true){
908
        if(addTimeStamp === true){
865 909
          var time = new Date();
866 910
          timestamp = time.getSeconds() + '.' + time.getMilliseconds() + 's';
867 911
        }
......
887 931
          }
888 932
        } else {
889 933
          // use the projection and maxextent of the base layer
890
          maxExtent = map.baseLayer.maxExtent;
934
          maxExtent = referenceMaxExtent();
891 935
        }
892 936

  
893 937
        if (maxExtent) {

Also available in: Unified diff