function search_bbox( boxen)
{
	var render=0;
	var use_new=0;
	if(document.getElementById("render").checked)
		render=1;
	var norm_bbox = JSON.parse(boxen);
	var timing={};
	/*This is for transforming to pixels, but here and now Leaflet takes care of that*/
//	var pixel_length=(norm_bbox[2]-norm_bbox[0])/s_w ;
	var pixel_length=0;
	var s_h=0;
	transform_values={"min_x":norm_bbox[0], "min_y":norm_bbox[1],"pixel_length":pixel_length, "screen_height":s_h};

	//Clean and remove the map when a redraw is needed
	kartan.clearLayers();
	map.removeLayer(kartan); 

	for (var layer in layers)
	{
		//Do the search in the spatial index
		var res=layers[layer].index.search(norm_bbox);
		
		//Order the result, or the reading in the twkb-file will be messed up
		res.sort(function(a,b){return(a[4]-b[4])});
		
		var len=res.length;
		var c = len;
		document.getElementById("antal").innerHTML="We have "+len+" geometries";
		timing.start=new Date().getTime();
		if(len>0)//If we had any hits.
		{
		var npoints=0;	
		//Now we are going to read the file in chunks. It is for memory reasons
			var hit_list=[];

			var antal = 0;

			var chunk=[-1,-1];//First value is start of chunk and last value is where chunk stops

			//Jump through the hit list
			for (var r=0;r<len;r++)
			{
				//Set the start of our chunk in the beginning of our first hit
				if(chunk[0]<0)
					chunk[0]=res[r][4];
					
				//Put the hit in a hit list
				hit_list.push(res[r][4]-chunk[0]); //we substract the length of the file that is before this chunk
				
				//If the next comming geometry gives a chunk bigger than the max_size that we defined in the "chunk_size" constant or if we have reached the end of the result list....
				if(((r+1)<len && ((res[r+1][4]+res[r+1][5])-chunk[0]>chunk_size))||(r==(len-1)))
				{
					//register the end of the bulk in the file
					chunk[1]=res[r][4]+res[r][5];
					
					//send the parsing work to a web worker
						var worker = webworkers.w[webworkers.n++] = new Worker('geom_worker.js');
					
					//When we get the coordinate list back from parsing...
						worker.onmessage = function(e) {
							if(e.data.type=="counter")
							{
								npoints+=e.data.npoints;
								if(--c==0)
								{
									timing.stop=new Date().getTime();
									diff=timing.stop-timing.start;
									document.getElementById("tid").innerHTML="Finnished in "+diff+" ms parsing "+npoints+ " vertexpoints in total";
								}
									
							}
							else if(e.data &&e.data.coords)
							{
								the_layer=e.data.layer;
								var id=e.data.id.toString();
								//Check the id against the attribute table for styling
								if(db.objectStoreNames.contains(the_layer))
								{
									var request = db.transaction([the_layer]).objectStore(the_layer).get(id);
								
								request.onerror=function(){
									alert("error");
								}
								request.onsuccess = function(happening) 
								{
									  if(happening.target.result)
										the_style= styles[the_layer].kode[happening.target.result.kod];
									  else
										the_style={smoothFactor:1,weight:1,color: '#000099',fillOpacity:0.1};
								  
								  
									if(e.data &&e.data.coords){
											switch (e.data.type){											
											case "line":
												kartan.addLayer(L.polyline(e.data.coords, the_style));
											break;
											case "polygon":
												kartan.addLayer(L.polygon(e.data.coords,the_style));//{smoothFactor:1,weight:1,color: 'red',fillOpacity:0.03}));
											break;
											}
										}
								}	
								}
								else
								{
										the_style={smoothFactor:1,weight:1,color: '#000099',fillOpacity:0.1};
								  
								  
									if(e.data &&e.data.coords){
											switch (e.data.type){											
											case "line":
												kartan.addLayer(L.polyline(e.data.coords, the_style));
											break;
											case "polygon":
												kartan.addLayer(L.polygon(e.data.coords,the_style));//{smoothFactor:1,weight:1,color: 'red',fillOpacity:0.03}));
											break;
											}
										}
								
								
								}
								
							};	
								if (e.data.type=="log")
									console.log(e.data.log);
						}						
					
					worker.postMessage({"the_file":layers[layer].the_file,"layer":layer,"chunk":chunk,"hit_list":hit_list,"transform_values":transform_values, "render":render});
				
				//if((r+1)<len)
					chunk[0]=-1;
					delete hit_list;
					var hit_list=[];
				
				}
			}
				kartan.addTo(map);
		}
	}	 
}
/*Here we get when the map is zoomed or panned if we are enough zoomed in*/
Here we get when the map is zoomed or panned if we are enough zoomed in
Here we get when the map is zoomed or panned if we are enough zoomed in
function search_bbox( boxen)
{
	var render=0;
	var use_new=0;
	if(document.getElementById("render").checked)
		render=1;
	var norm_bbox = JSON.parse(boxen);
	var timing={};
	var pixel_length=0;
	var s_h=0;
	transform_values={"min_x":norm_bbox[0], "min_y":norm_bbox[1],"pixel_length":pixel_length, "screen_height":s_h};
	kartan.clearLayers();
	map.removeLayer(kartan); 
	for (var layer in layers)
	{
		var res=layers[layer].index.search(norm_bbox);
		res.sort(function(a,b){return(a[4]-b[4])});
		var len=res.length;
		var c = len;
		document.getElementById("antal").innerHTML="We have "+len+" geometries";
		timing.start=new Date().getTime();
		if(len>0)
		{
		var npoints=0;	
			var hit_list=[];
			var antal = 0;
			var chunk=[-1,-1];
			for (var r=0;r<len;r++)
			{
				if(chunk[0]<0)
					chunk[0]=res[r][4];
				hit_list.push(res[r][4]-chunk[0]); 
				if(((r+1)<len && ((res[r+1][4]+res[r+1][5])-chunk[0]>chunk_size))||(r==(len-1)))
				{
					chunk[1]=res[r][4]+res[r][5];
						var worker = webworkers.w[webworkers.n++] = new Worker('geom_worker.js');
						worker.onmessage = function(e) {
							if(e.data.type=="counter")
							{
								npoints+=e.data.npoints;
								if(--c==0)
								{
									timing.stop=new Date().getTime();
									diff=timing.stop-timing.start;
									document.getElementById("tid").innerHTML="Finnished in "+diff+" ms parsing "+npoints+ " vertexpoints in total";
								}
							}
							else if(e.data &&e.data.coords)
							{
								the_layer=e.data.layer;
								var id=e.data.id.toString();
								if(db.objectStoreNames.contains(the_layer))
								{
									var request = db.transaction([the_layer]).objectStore(the_layer).get(id);
								request.onerror=function(){
									alert("error");
								}
								request.onsuccess = function(happening) 
								{
									  if(happening.target.result)
										the_style= styles[the_layer].kode[happening.target.result.kod];
									  else
										the_style={smoothFactor:1,weight:1,color: '#000099',fillOpacity:0.1};
									if(e.data &&e.data.coords){
											switch (e.data.type){											
											case "line":
												kartan.addLayer(L.polyline(e.data.coords, the_style));
											break;
											case "polygon":
												kartan.addLayer(L.polygon(e.data.coords,the_style));
											break;
											}
										}
								}	
								}
								else
								{
										the_style={smoothFactor:1,weight:1,color: '#000099',fillOpacity:0.1};
									if(e.data &&e.data.coords){
											switch (e.data.type){											
											case "line":
												kartan.addLayer(L.polyline(e.data.coords, the_style));
											break;
											case "polygon":
												kartan.addLayer(L.polygon(e.data.coords,the_style));
											break;
											}
										}
								}
							};	
								if (e.data.type=="log")
									console.log(e.data.log);
						}						
					worker.postMessage({"the_file":layers[layer].the_file,"layer":layer,"chunk":chunk,"hit_list":hit_list,"transform_values":transform_values, "render":render});
					chunk[0]=-1;
					delete hit_list;
					var hit_list=[];
				}
			}
				kartan.addTo(map);
		}
	}	 
}
function search_bbox ( boxen ) { var render = 0 ; var use_new = 0 ; if ( document . getElementById ( "render" ) . checked ) render = 1 ; var norm_bbox = JSON . parse ( boxen ) ; var timing = { } ; var pixel_length = 0 ; var s_h = 0 ; transform_values = { "min_x" : norm_bbox [ 0 ] , "min_y" : norm_bbox [ 1 ] , "pixel_length" : pixel_length , "screen_height" : s_h } ; kartan . clearLayers ( ) ; map . removeLayer ( kartan ) ; for ( var layer in layers ) { var res = layers [ layer ] . index . search ( norm_bbox ) ; res . sort ( function ( a , b ) { return ( a [ 4 ] - b [ 4 ] ) } ) ; var len = res . length ; var c = len ; document . getElementById ( "antal" ) . innerHTML = "We have " + len + " geometries" ; timing . start = new Date ( ) . getTime ( ) ; if ( len > 0 ) { var npoints = 0 ; var hit_list = [ ] ; var antal = 0 ; var chunk = [ - 1 , - 1 ] ; for ( var r = 0 ; r < len ; r ++ ) { if ( chunk [ 0 ] < 0 ) chunk [ 0 ] = res [ r ] [ 4 ] ; hit_list . push ( res [ r ] [ 4 ] - chunk [ 0 ] ) ; if ( ( ( r + 1 ) < len && ( ( res [ r + 1 ] [ 4 ] + res [ r + 1 ] [ 5 ] ) - chunk [ 0 ] > chunk_size ) ) || ( r == ( len - 1 ) ) ) { chunk [ 1 ] = res [ r ] [ 4 ] + res [ r ] [ 5 ] ; var worker = webworkers . w [ webworkers . n ++ ] = new Worker ( 'geom_worker.js' ) ; worker . onmessage = function ( e ) { if ( e . data . type == "counter" ) { npoints += e . data . npoints ; if ( -- c == 0 ) { timing . stop = new Date ( ) . getTime ( ) ; diff = timing . stop - timing . start ; document . getElementById ( "tid" ) . innerHTML = "Finnished in " + diff + " ms parsing " + npoints + " vertexpoints in total" ; } } else if ( e . data && e . data . coords ) { the_layer = e . data . layer ; var id = e . data . id . toString ( ) ; if ( db . objectStoreNames . contains ( the_layer ) ) { var request = db . transaction ( [ the_layer ] ) . objectStore ( the_layer ) . get ( id ) ; request . onerror = function ( ) { alert ( "error" ) ; } request . onsuccess = function ( happening ) { if ( happening . target . result ) the_style = styles [ the_layer ] . kode [ happening . target . result . kod ] ; else the_style = { smoothFactor : 1 , weight : 1 , color : '#000099' , fillOpacity : 0.1 } ; if ( e . data && e . data . coords ) { switch ( e . data . type ) { case "line" : kartan . addLayer ( L . polyline ( e . data . coords , the_style ) ) ; break ; case "polygon" : kartan . addLayer ( L . polygon ( e . data . coords , the_style ) ) ; break ; } } } } else { the_style = { smoothFactor : 1 , weight : 1 , color : '#000099' , fillOpacity : 0.1 } ; if ( e . data && e . data . coords ) { switch ( e . data . type ) { case "line" : kartan . addLayer ( L . polyline ( e . data . coords , the_style ) ) ; break ; case "polygon" : kartan . addLayer ( L . polygon ( e . data . coords , the_style ) ) ; break ; } } } } ; if ( e . data . type == "log" ) console . log ( e . data . log ) ; } worker . postMessage ( { "the_file" : layers [ layer ] . the_file , "layer" : layer , "chunk" : chunk , "hit_list" : hit_list , "transform_values" : transform_values , "render" : render } ) ; chunk [ 0 ] = - 1 ; delete hit_list ; var hit_list = [ ] ; } } kartan . addTo ( map ) ; } } }
Here we get when the map is zoomed or panned if we are enough zoomed in
Here we get when the map is zoomed or panned if we are enough zoomed in
/*This is for transforming to pixels, but here and now Leaflet takes care of that*/ //	var pixel_length=(norm_bbox[2]-norm_bbox[0])/s_w ; //Clean and remove the map when a redraw is needed //Do the search in the spatial index //Order the result, or the reading in the twkb-file will be messed up //If we had any hits. //Now we are going to read the file in chunks. It is for memory reasons //First value is start of chunk and last value is where chunk stops //Jump through the hit list //Set the start of our chunk in the beginning of our first hit //Put the hit in a hit list //we substract the length of the file that is before this chunk //If the next comming geometry gives a chunk bigger than the max_size that we defined in the "chunk_size" constant or if we have reached the end of the result list.... //register the end of the bulk in the file //send the parsing work to a web worker //When we get the coordinate list back from parsing... //Check the id against the attribute table for styling //{smoothFactor:1,weight:1,color: 'red',fillOpacity:0.03})); //{smoothFactor:1,weight:1,color: 'red',fillOpacity:0.03})); //if((r+1)<len)