WME Belgium-SL

Creates polygons for Cities in a WME "Belgium-SL" layer

// ==UserScript==
// @name                WME Belgium-SL
// @namespace           https://greasyfork.org/en/users/8063-mo
// @description         Creates polygons for Cities in a WME "Belgium-SL" layer
// @include             https://www.waze.com/editor/*
// @include             https://www.waze.com/*/editor/*
// @include             https://editor-beta.waze.com/*
// @version             2.2
// @grant               none
// @copyright           2015 rickzabel based off work by 2014 davielde
// ==/UserScript==


//---------------------------------------------------------------------------------------

//generated by rickzabel's overlay generator
//special thanks to t0cableguy for helping with the census data

//RZ RaidName will be replaced by the name of the layer in your KML file
//RZ RaidNameNoSpaces will be replaced by the name of the layer in your KML file
//RZ AreaPoints will be replaced by the names, colors, and area points from your KML file

setTimeout(InitMapRaidOverlay, 1000);

function AddRaidPolygon(raidLayer,groupPoints,groupColor,groupNumber){
    
    var mro_Map = Waze.map;
    var mro_OL = OpenLayers;
    var raidGroupLabel = groupNumber;
    var groupName = groupNumber;
    
    var style = {
        strokeColor: "Pink",
        strokeOpacity: .8,
        strokeWidth: 10,
        fillColor: "red",
        fillOpacity: 0.08,
        label: raidGroupLabel,
        labelOutlineColor: "Black",
        labelOutlineWidth: 5,
        fontSize: 24,
        fontColor: "Pink",
        fontOpacity: .90,
        fontWeight: "bold"  
    };
	
    var attributes = {
        name: groupName,
        number: groupNumber,
		holes: "false"
	};
    
    var pnt= [];
    for(i=0;i<groupPoints.length;i++){
        convPoint = new OpenLayers.Geometry.Point(groupPoints[i].lon,groupPoints[i].lat).transform(new OpenLayers.Projection("EPSG:4326"), mro_Map.getProjectionObject());
        //console.log('MapRaid: ' + JSON.stringify(groupPoints[i]) + ', ' + groupPoints[i].lon + ', ' + groupPoints[i].lat);
        pnt.push(convPoint);
    }
		       
    var ring = new mro_OL.Geometry.LinearRing(pnt);
    var polygon = new mro_OL.Geometry.Polygon([ring]);
    
    var feature = new mro_OL.Feature.Vector(polygon,attributes,style);
    raidLayer.addFeatures([feature]);

}

function CurrentRaidLocation(raid_mapLayer){
    var mro_Map = Waze.map;

    for(i=0;i<raid_mapLayer.features.length;i++){
        var raidMapCenter = mro_Map.getCenter();
        var raidCenterPoint = new OpenLayers.Geometry.Point(raidMapCenter.lon,raidMapCenter.lat);
        var raidCenterCheck = raid_mapLayer.features[i].geometry.components[0].containsPoint(raidCenterPoint);
		var holes = raid_mapLayer.features[i].attributes.holes
		
        
        if(raidCenterCheck === true){

			var str = $('#topbar-container > div > div.location-info-region > div').text();
			
			var n2 = str.indexOf(" - ");
			
			if(n2 > 0){
				var n = str.length;
				var res = str.substring(n2+2, n);
				var rescount = res.indexOf(" - ");
				if(rescount>0){
					var n3 = res.length;
					var res2 = res.substring(rescount+2, n3);
				}
				var raidLocationLabel = '['+ raid_mapLayer.features[i].attributes.number + '] - ' + res2;

			} else {
				var raidLocationLabel = '['+ raid_mapLayer.features[i].attributes.number + '] - ' + $('#topbar-container > div > div.location-info-region > div').text();
						
			}	
			setTimeout(function(){$('#topbar-container > div > div.location-info-region > div').text(raidLocationLabel);},200);
			 if (holes === "false") { break; }
		}
    }
}

function generateholes(mro_Map, raid_mapLayer, UntitledPolygon){
	var mro_mapLayers = mro_Map.getLayersBy("uniqueName","__Tennessee");
	var raid_mapLayerLength = raid_mapLayer.features.length;	
	
	var style = raid_mapLayer.features[raid_mapLayerLength-2].style;
	var attributes = raid_mapLayer.features[raid_mapLayerLength-2].attributes;
	
	//console.dir(style);	
	//console.dir(attributes);	
	
	var newFeature = Subtract(raid_mapLayer.features[raid_mapLayerLength-2], raid_mapLayer.features[1]);
    
	//console.dir(raid_mapLayer);	
	
	raid_mapLayer.addFeatures([newFeature]);

    raid_mapLayer.features[raid_mapLayerLength-1].destroy();
	raid_mapLayer.features[raid_mapLayerLength-2].destroy();
		
	//console.dir(mro_mapLayers);	
	var mro_mapLayersFeaturesLength = mro_mapLayers[0].features.length;
    mro_mapLayers[0].features[mro_mapLayersFeaturesLength-1].style =  style;
	mro_mapLayers[0].features[mro_mapLayersFeaturesLength-1].attributes = attributes;
    mro_mapLayers[0].features[mro_mapLayersFeaturesLength-1].attributes.holes = "true";
	//raid_mapLayer.features[1].style = {label: "Updated label"};
    mro_mapLayers[0].redraw(); 
	
	//console.dir(raid_mapLayer);	
}

function Subtract(bigFeature, smallFeature) {

    var newPolygon = new OpenLayers.Geometry.Polygon(bigFeature.geometry.components);
    var newFeature = new OpenLayers.Feature.Vector(newPolygon);

    newPolygon.addComponent(smallFeature.geometry.components[0]);

    return newFeature;
}


function InitMapRaidOverlay(){

    var mro_Map = Waze.map;
    var mro_OL = OpenLayers;

    //if (!mro_Map) return;
    //if (!mro_OL) return;

    var mro_mapLayers = mro_Map.getLayersBy("uniqueName","Belgium-SL");
        
    var raid_mapLayer = new mro_OL.Layer.Vector("Belgium-SL", {
        displayInLayerSwitcher: true,
        uniqueName: "Belgium-SL"
    });
        
    I18n.translations.en.layers.name["Belgium-SL"] = "Belgium-SL";
    mro_Map.addLayer(raid_mapLayer);
    raid_mapLayer.setVisibility(true);
    

var VWallonia         = [{lon:'3.1997681',lat:'50.760784699999995'},{lon:'3.244392',lat:'50.70448669999999'},{lon:'3.2943179000000002',lat:'50.5158258'},{lon:'3.6169333999999997',lat:'50.4858786'},{lon:'3.6941528000000003',lat:'50.3139004'},{lon:'4.0156643',lat:'50.3492085'},{lon:'4.1940308',lat:'50.2647654'},{lon:'4.1802979',lat:'49.9635891'},{lon:'4.475287799999999',lat:'49.9466394'},{lon:'4.8346199',lat:'50.1353373'},{lon:'4.8628757',lat:'49.7962294'},{lon:'5.1837728',lat:'49.70101890000001'},{lon:'5.4670716',lat:'49.5091601'},{lon:'5.869445800000001',lat:'49.573321400000005'},{lon:'5.767822300000001',lat:'49.9105558'},{lon:'6.3528442',lat:'50.3857566'},{lon:'6.1248779',lat:'50.713852'},{lon:'5.679931599999999',lat:'50.7972555'},{lon:'3.938598599999999',lat:'50.69123810000001'},{lon:'3.1997681',lat:'50.760784699999995'}];
AddRaidPolygon(raid_mapLayer, VWallonia,"#            000000","            Wallonia");


    
	
	
    setTimeout(function(){CurrentRaidLocation(raid_mapLayer);},3000);
    mro_Map.events.register("moveend", Waze.map, function(){ setTimeout(function(){CurrentRaidLocation(raid_mapLayer);},1500);});
    mro_Map.events.register("zoomend", Waze.map, function(){ setTimeout(function(){CurrentRaidLocation(raid_mapLayer);},1500);});
       
       
}