WME Turkey-Denizli

Creates polygons for Cities in a WME "Turkey-Denizli" layer

// ==UserScript==
// @name                WME Turkey-Denizli
// @namespace           https://greasyfork.org/en/users/8063-mo
// @description         Creates polygons for Cities in a WME "Turkey-Denizli" 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: groupColor,
        fillOpacity: 0.10,
        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 = '[City - ' + raid_mapLayer.features[i].attributes.number + '] - ' + res2;

			} else {
				var raidLocationLabel = '[City - ' + 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","__Turkey-Denizli");
        
    var raid_mapLayer = new mro_OL.Layer.Vector("Turkey-Denizli", {
        displayInLayerSwitcher: true,
        uniqueName: "__Turkey-Denizli"
    });
        
    I18n.translations.en.layers.name["__Turkey-Denizli"] = "Turkey-Denizli";
    mro_Map.addLayer(raid_mapLayer);
    raid_mapLayer.setVisibility(true);
    

var VDenizli = [{lon:'28.5823059',lat:'37.5271536'},{lon:'29.105529799999992',lat:'36.87083220000001'},{lon:'29.2877',lat:'36.859'},{lon:'29.6795654',lat:'37.3941641'},{lon:'29.548400000000004',lat:'37.5207'},{lon:'29.716399999999997',lat:'37.6263'},{lon:'29.954223600000006',lat:'37.7945928'},{lon:'29.731750500000004',lat:'37.9723499'},{lon:'30.173950200000004',lat:'38.2575931'},{lon:'29.7962',lat:'38.504000000000005'},{lon:'28.8438',lat:'38.2335'},{lon:'28.6326',lat:'38.0743'},{lon:'28.7265015',lat:'37.8564229'},{lon:'28.818512000000002',lat:'37.7685436'},{lon:'28.5823059',lat:'37.5271536'}];
AddRaidPolygon(raid_mapLayer, VDenizli,"#A61B4A","Denizli");


    
	
	
    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);});
       
       
}