2016-08-30 3 views
1

OSM 맵에서 원하는 영역을 윤곽을 그리기위한 많은 다각형을 포함하는 벡터 레이어가 있습니다. 사용자는 특정 관심 영역에 대한 추가 정보를 얻기 위해 맵에 테두리 상자를 그릴 수 있습니다. 확인해야 할 것은 사용자가 그린 바운딩 박스가 벡터 레이어의 많은 폴리곤 중 하나에 있는지 여부입니다. 오픈 레이어 3에서 가능합니까? 익스텐트를 비교할 수 있지만 벡터 레이어의 범위는 전체 맵입니다. 상자가 작은 영역에 있는지 알아야합니다. 누구나 알고 있나요 .... 나는이 지역의 좌표로 파고, 범위를 비교 해봤지만 배열의 배열의 배열,이다Openlayers의 벡터 레이어 내에 경계 상자가 있는지 확인하십시오. 3

map = new ol.Map({ 
     interactions: ol.interaction.defaults({ 
      keyboard: false, 
      altShiftDragRotate: false 
     }), 
     target: 'create-export-map', 
     view: new ol.View({ 
      projection: "EPSG:4326", 
      //extent: [-180,-90,180,90], 
      center: [44.4333, 33.3333], 
      zoom: 4, 
      maxZoom: 18, 
     }) 
    }) 


    //add base layers 
    var osm = new ol.layer.Tile({ 
     title: "OpenStreetMap", 
     source: new ol.source.OSM({ 
      wrapX: false, 
      noWrap: true, 
      attributions: [ 
       new ol.Attribution({ 
        html: '&copy ' + 
        '<a href="//www.openstreetmap.org/copyright">OpenStreetMap</a> contributors.' 
       }), 

       ol.source.OSM.ATTRIBUTION 
      ] 

     }) 
    }) 

    map.addLayer(osm); 

regionsSource = new ol.source.Vector({ 
     wrapX: false, 
     noWrap: true, 
    }); 


    regions = new ol.layer.Vector({ 
      name: 'regions', 
      source: regionsSource, 
      style: new ol.style.Style({ 
       fill: new ol.style.Fill({ 
        color: 'rgba(0,0,0,0)', 
        //opacity: 0.8, 
       }), 
       stroke: new ol.style.Stroke({ 
        color: 'rgba(215, 63, 63, 0.8)', 
        width: 3.5, 
       }) 
      }), 

     }) 
    map.addLayer(regions); 

$.getJSON(jsonfileURL, function(data){ 
     var geojson = new ol.format.GeoJSON(); 
     var features = geojson.readFeatures(data); 
     regionsSource.addFeatures(features); 
     var extent = regionsSource.getExtent(); 

     map.getView().fit(extent, map.getSize());    
    }); 

//OL3 add bounding box selection layer 
    bboxSource = new ol.source.Vector() 
    bbox = new ol.layer.Vector({ 
     name: 'Select', 
     source: bboxSource, 
     style: new ol.style.Style({ 
      stroke: new ol.style.Stroke({ 
       color: 'blue' 
      }), 
      fill: new ol.style.Fill({ 
       color: [0, 0, 255, 0.05] 
      }) 
     }) 
    }); 
    map.addLayer(bbox); 

//Map control for dragbox - bounding box 
var dragBox = new ol.interaction.DragBox({ 
     condition: ol.events.condition.primaryAction, 
    }); 

    var translate; 

    dragBox.on('boxend', function(e){ 
     var dragFeature = new ol.Feature({ 
      geometry: dragBox.getGeometry() 
     }); 
     bboxSource.addFeature(dragFeature); 
     map.removeInteraction(dragBox); 
     translate = new ol.interaction.Translate({ 
      features: new ol.Collection([dragFeature]) 
     }); 

     var bounds = dragFeature.getGeometry().getExtent(); 
     filtering = true; 

     // validate the selected extents 
     if (validateBounds(bounds)) { 
      setBounds(bounds); 
     } 
     else { 
      unsetBounds(); 
     } 

function validateBounds(bounds) { 
    var regions, region; 
    map.getLayers().forEach(function (l) { 
     if (l.get('name') == 'regions') 
      regions = l; 
    }) 

    var valid_region = false; 

    // check that we're within a polygon region. 
    //This is where I'm stuck... 
} 

:

지도 및 벡터 레이어 포함 geojson 설정 bbox가 Regions 레이어의 폴리곤 중 하나에 속하는지 확인하는 좋은 방법은 무엇입니까?

for (i = 0; i < regions.length; i++){ 
     region = regions[i].geometry; 
     if (extent.intersects(region)){ 
      valid_region = true; 
     } 

} 사전에

감사 : 나는 OpenLayers에서 3.17.1

openlayers-2는 다음과 같이 이루어졌다를 사용하고 있습니다!

답변

0

이와 비슷한?

for (i = 0; i < regions.length; i++) { 
    var region = regions[i]; 
    for (j = 0; j < region.getSource().getFeatures().length; j++) { 
     var feature = regions.getSource().getFeatures()[j]; 
     if (ol.extent.intersects(extent,feature.getGeometry().getExtent())) { 
      /* do whatever you want */ 
     } 
    } 
}