function Generic_GIS_Utils(){this._center_point=null}Generic_GIS_Utils.prototype.compute_versor_vector=function(c){var b=Math.sqrt(Math.pow(c[0],2)+Math.pow(c[1],2));var a=new Array(c[0]*1/b,c[1]*1/b);return a};Generic_GIS_Utils.prototype.compute_direction_vector=function(b,a){return new Array(a._x-b._x,a._y-b._y)};Generic_GIS_Utils.prototype.compute_distance_between_points=function(b,a){return Math.sqrt(Math.pow(a._x-b._x,2)+Math.pow(a._y-b._y,2))};Generic_GIS_Utils.prototype.vectors_angle=function(e,d){var a=e[0]*d[0]+e[1]*d[1];var b=Math.sqrt(Math.pow(e[0],2)+Math.pow(e[1],2));var c=Math.sqrt(Math.pow(d[0],2)+Math.pow(d[1],2));return Math.acos(a/(b*c))};Generic_GIS_Utils.prototype.are_lines_parallel=function(b,a){var c=this.vectors_angle(this.compute_direction_vector(b._p1,b._p2),this.compute_direction_vector(a._p1,a._p2));return(isNaN(c)||c==0||new Number(c).toFixed(3)==new Number(Math.PI).toFixed(3))};Generic_GIS_Utils.prototype.intersect_lines=function(g,f){try{var i=g._p1.clone();var h=f._p1.clone();var c=this.compute_direction_vector(g._p1,g._p2);var b=this.compute_direction_vector(f._p1,f._p2);var j=((h._x-i._x)*c[1]+(i._y-h._y)*c[0])/((c[0]*b[1])-(c[1]*b[0]));var a=new Point(h._x+j*b[0],h._y+j*b[1]);return a}catch(d){throw"Generic_GIS_Utils.intersect_line(): "+d}};Generic_GIS_Utils.prototype.intersect_circle=function(e,b){var s=e._center._x;var g=e._center._y;var p=e._radius;var r=b._center._x;var f=b._center._y;var n=b._radius;var u,m,l,t,o,j,i;var q,c;var k=new Array;m=r-s;l=f-g;t=Math.sqrt((l*l)+(m*m));if(t>(p+n)){return 0}if(t<Math.abs(p-n)){return k}u=((p*p)-(n*n)+(t*t))/(2*t);q=s+(m*u/t);c=g+(l*u/t);o=Math.sqrt((p*p)-(u*u));j=-l*(o/t);i=m*(o/t);if(((q+j)!=(q-j))&&((c+i)!=(c-i))){k[0]=new Point(q+j,c+i);k[1]=new Point(q-j,c-i)}else{k[0]=new Point(q+j,c+i)}return k};Generic_GIS_Utils.prototype.line_intersect_circle=function(t,e){var k,j,i;var f;var g=new Point();var q=t._p1;var p=t._p2;var h=e._center;var d=e._radius;var o,m;g._x=p._x-q._x;g._y=p._y-q._y;k=g._x*g._x+g._y*g._y;j=2*(g._x*(q._x-h._x)+g._y*(q._y-h._y));i=h._x*h._x+h._y*h._y;i+=q._x*q._x+q._y*q._y;i-=2*(h._x*q._x+h._y*q._y);i-=d*d;f=j*j-4*k*i;if(f<0){o=0;m=0;return false}o=(-j+Math.sqrt(f))/(2*k);m=(-j-Math.sqrt(f))/(2*k);var n=new Point((q._x+o*(p._x-q._x)),(q._y+o*(p._y-q._y)));var l=new Point((q._x+m*(p._x-q._x)),(q._y+m*(p._y-q._y)));var s=new Array();s[0]=n;s[1]=l;return s};Generic_GIS_Utils.prototype.decode_rows_from_xml=function(k){var g=k.getElementsByTagName("row");var e=new Array();for(var d=0;d<g.length;d++){var l=g[d];var c=l.getElementsByTagName("attribute");l=new Row();for(var j=0;j<c.length;j++){var f=c[j];var a=f.getElementsByTagName("name")[0].firstChild.nodeValue;var h=f.getElementsByTagName("value")[0].hasChildNodes()?f.getElementsByTagName("value")[0].firstChild.nodeValue:"";var b=new Attribute(a,"",h);l.add_attribute(b)}e.push(l)}return e};Generic_GIS_Utils.prototype.decode_geometry_from_xml=function(a){var b=a.getElementsByTagName("geometry_type")[0].firstChild.nodeValue;var c=null;if(b=="POINT"){c=_GENERIC_GIS_UTILS.decode_point_from_xml(a)}else{if(b=="MULTICURVE"){_LINE_PATH=_MAP_WRAPPER.decode_multi_curve_from_xml(a);c=_LINE_PATH}else{if(b=="MULTISURFACE"){_LINE_PATH=_MAP_WRAPPER.decode_multi_surface_from_xml(a);c=_LINE_PATH}}}return c};Generic_GIS_Utils.prototype.process_already_selected_geometry=function(b){var c=false;for(var a=0;a<_LIST_OF_SELECTED_OBJECTS.length;a++){if(_LIST_OF_SELECTED_OBJECTS[a].constructor==b.constructor&&_LIST_OF_SELECTED_OBJECTS[a]._id_geometry==b._id_geometry&&_LIST_OF_SELECTED_OBJECTS[a]._layer_name==b._layer_name){_LIST_OF_SELECTED_OBJECTS.splice(a,1);c=true;break}}for(var a=0;a<_LIST_OF_EXTRA_GRAPHICS.length;a++){if(_LIST_OF_EXTRA_GRAPHICS[a].constructor==b.constructor&&_LIST_OF_EXTRA_GRAPHICS[a]._id_geometry==b._id_geometry&&_LIST_OF_EXTRA_GRAPHICS[a]._layer_name==b._layer_name){remove_from_extra_graphics(_LIST_OF_EXTRA_GRAPHICS[a],_SELECTION_DRAWING_ENGINE);c=true;break}}return c};Generic_GIS_Utils.prototype.decode_point_from_xml=function(c){var b=c.getElementsByTagName("id_geometry")[0].hasChildNodes()?c.getElementsByTagName("id_geometry")[0].firstChild.nodeValue:"";var a=new Point(c.getElementsByTagName("point")[0].getElementsByTagName("x")[0].firstChild.nodeValue,c.getElementsByTagName("point")[0].getElementsByTagName("y")[0].firstChild.nodeValue);a._id_geometry=b;a._type="POINT";return a};Generic_GIS_Utils.prototype.are_points_on_same_line=function(a,c,b){if(Math.abs((a._x-b._x)*(c._y-b._y)-(a._y-b._y)*(c._x-b._x))<0.1){return true}else{return false}};Generic_GIS_Utils.prototype.calculate_radius=function(h,g,f){var e=h.distance(g);var d=g.distance(f);var i=f.distance(h);return(e*d*i)/Math.sqrt((e+d+i)*(d+i-e)*(i+e-d)*(e+d-i))};Generic_GIS_Utils.prototype.det=function(l,b){try{var g,f,p,o;var k=0;var c=new Array();c[0]=new Array();c[1]=new Array();c[2]=new Array();if(b==2){k=l[0][0]*l[1][1]-l[1][0]*l[0][1]}else{k=0}for(p=0;p<b;p++){for(g=1;g<b;g++){o=0;for(f=0;f<b;f++){if(f==p){continue}c[g-1][o]=l[g][f];o++}}k=k+Math.pow((-1),(0+p))*l[0][p]*this.det(c,b-1)}return k}catch(h){alert("Generic_GIS_Utils.det() :"+h);return false}};Generic_GIS_Utils.prototype.encode_save_polygon_to_http=function(){try{_LINE_PATH.close_ring();var a="";if(_LINE_PATH._list_of_graphics[0] instanceof Line_Path){var d=true;for(var b=0;b<_LINE_PATH._list_of_graphics.length;b++){_LINE_PATH._list_of_graphics[b].close_ring();if(d){a=_LINE_PATH._list_of_graphics[b].to_post_request();d=false}else{a+="|"+_LINE_PATH._list_of_graphics[b].to_post_request()}}if(_LINE_PATH_ISLAND!=null&&_LINE_PATH_ISLAND._list_of_graphics.length!=0){_LINE_PATH_ISLAND.close_ring();a+="|"+_LINE_PATH_ISLAND.to_post_request()}}else{a=_LINE_PATH.to_post_request()}clear_extra_graphics(_ALL_DRAWING_ENGINES);draw_extra_graphics(false,_ALL_DRAWING_ENGINES);return a}catch(c){throw c}};Generic_GIS_Utils.prototype.encode_update_polygon_to_http=function(){try{var a="";if(_LINE_PATH._list_of_graphics[0] instanceof Line_Path){var d=true;for(var b=0;b<_LINE_PATH._list_of_graphics.length;b++){_LINE_PATH._list_of_graphics[b].close_ring();if(d){a=_LINE_PATH._list_of_graphics[b].to_post_request();d=false}else{a+="|"+_LINE_PATH._list_of_graphics[b].to_post_request()}}if(_LINE_PATH_ISLAND!=null&&_LINE_PATH_ISLAND._list_of_graphics.length!=0){_LINE_PATH_ISLAND.close_ring();a+="|"+_LINE_PATH_ISLAND.to_post_request()}}else{_LINE_PATH.close_ring();a=_LINE_PATH.to_post_request()}clear_extra_graphics(_ALL_DRAWING_ENGINES);draw_extra_graphics(false,_ALL_DRAWING_ENGINES);return a}catch(c){throw c}};Generic_GIS_Utils.prototype.is_line_path_drawn=function(){if(_LINE_PATH==null||_LINE_PATH._list_of_graphics.length==0){return false}return true};Generic_GIS_Utils.prototype.is_line_path_closed=function(){if(_LINE_PATH!=null&&_LINE_PATH._list_of_graphics.length!=0){if(_LINE_PATH._list_of_graphics[0] instanceof Line_Path){if(_LINE_PATH._list_of_graphics[0]._list_of_graphics[0]._p1.equals(_LINE_PATH._list_of_graphics[0]._list_of_graphics[_LINE_PATH._list_of_graphics[0]._list_of_graphics.length-1]._p2)){return true}}}return false};Generic_GIS_Utils.prototype.is_line_path_selected=function(){if(_LINE_PATH==null||_LINE_PATH._list_of_graphics.length==0||_LIST_OF_SELECTED_OBJECTS.length!=1||_LIST_OF_SELECTED_OBJECTS[0] instanceof Point||_LIST_OF_SELECTED_OBJECTS[0]._type!="MULTICURVE"){return false}return true};Generic_GIS_Utils.prototype.is_multi_surface_selected=function(){if(_LINE_PATH==null||_LINE_PATH._list_of_graphics.length==0||_LIST_OF_SELECTED_OBJECTS.length!=1||_LIST_OF_SELECTED_OBJECTS[0] instanceof Point||_LIST_OF_SELECTED_OBJECTS[0]._type!="MULTISURFACE"||_LIST_OF_SELECTED_OBJECTS[0]._id_geometry==null){return false}return true};Generic_GIS_Utils.prototype.encode_save_line_to_http=function(){try{var a="";if(_LINE_PATH._list_of_graphics[0] instanceof Line_Path){var d=true;for(var b=0;b<_LINE_PATH._list_of_graphics.length;b++){if(d){a=_LINE_PATH._list_of_graphics[b].to_post_request();d=false}else{a+="|"+_LINE_PATH._list_of_graphics[b].to_post_request()}}if(_LINE_PATH_ISLAND!=null&&_LINE_PATH_ISLAND._list_of_graphics.length!=0){a+="|"+_LINE_PATH_ISLAND.to_post_request()}}else{a=_LINE_PATH.to_post_request()}return a}catch(c){throw"Generic_GIS_Utils.encode_save_line_to_http(): "+c}};Generic_GIS_Utils.prototype.encode_update_line_to_http=function(){try{var a="";if(_LINE_PATH._list_of_graphics[0] instanceof Line_Path){var d=true;for(var b=0;b<_LINE_PATH._list_of_graphics.length;b++){if(d){a=_LINE_PATH._list_of_graphics[b].to_post_request();d=false}else{a+="|"+_LINE_PATH._list_of_graphics[b].to_post_request()}}if(_LINE_PATH_ISLAND!=null&&_LINE_PATH_ISLAND._list_of_graphics.length!=0){a+="|"+_LINE_PATH_ISLAND.to_post_request()}}else{a=_LINE_PATH.to_post_request()}return a}catch(c){throw"Generic_GIS_Utils.encode_update_line_to_http(): "+c}};Generic_GIS_Utils.prototype.is_point_defined=function(){if(_LIST_OF_POINTS.length==1){return true}if(_LIST_OF_SELECTED_OBJECTS.length>0){var b=0;for(var a=0;a<_LIST_OF_SELECTED_OBJECTS.length;a++){if(_LIST_OF_SELECTED_OBJECTS[a] instanceof Point){b++}}if(b==1){return true}}return false};Generic_GIS_Utils.prototype.is_point_selected=function(){if(_LIST_OF_SELECTED_OBJECTS.length!=1||_LIST_OF_SELECTED_OBJECTS[0] instanceof Line_Path){return false}return true};Generic_GIS_Utils.prototype.encode_point_to_http=function(){if(_LIST_OF_POINTS.length==1){return"x="+_LIST_OF_POINTS[0]._x+"&y="+_LIST_OF_POINTS[0]._y}else{if(_LIST_OF_SELECTED_OBJECTS.length==1&&_LIST_OF_SELECTED_OBJECTS[0] instanceof Point){return"x="+_LIST_OF_SELECTED_OBJECTS[0]._x+"&y="+_LIST_OF_SELECTED_OBJECTS[0]._y}else{throw"Generic_GIS_Utils.encode_point_to_http(): point not defined"}}};Generic_GIS_Utils.prototype.open_new_window=function(a,c,k,h,e){var g=_GUI_FACTORY.create_gui_command("create_window");g._url=a;g._width=c;g._height=k;g._navigation_menu=h;g._overwrite_global=e;g.execute();var i=new Soft_Gis_Window();var f=_GUI_FACTORY.create_gui_command("reload_window");f._window_object=g._window_object;i._reload_command=f;var d=_GUI_FACTORY.create_gui_command("move_window");d._window_object=g._window_object;i._move_command=d;var b=_GUI_FACTORY.create_gui_command("register_close_handler_window");b._window_object=g._window_object;i._register_on_close_command=b;var j=_GUI_FACTORY.create_gui_command("close_window");j._window_object=g._window_object;i._close_command=j;if(e){if(_WIN){_WIN.close()}_WIN=i}return i};Generic_GIS_Utils.prototype.reverse_points=function(){_LIST_OF_POINTS.reverse()};Generic_GIS_Utils.prototype.snap_point_on_layers=function(d){try{var a=false;for(var b=0;b<_MAP_WRAPPER._list_of_layers.length;b++){if(_MAP_WRAPPER._list_of_layers[b]._snap){a=true}}if(a){clearTimeout(_TIMEOUT_FUNCTION);_TIMEOUT_FUNCTION=window.setTimeout(function(){try{var h="type=layer&action=snap_point&x="+d[0]+"&y="+d[1]+"&layers=";for(var f=0;f<_MAP_WRAPPER._list_of_layers.length;f++){if(_MAP_WRAPPER._list_of_layers[f]._snap){h+=_MAP_WRAPPER._list_of_layers[f]._name+"#"}}process_ajax_call(h,_CNTR_LAYER_EDITOR,function(i){try{if(!i){var k=_MAP_WRAPPER.decode_points_from_xml(this.responseXML)[0];var l=_MAP_WRAPPER.convert_to_pixel(k._x,k._y,true);if(k._snap_type==_SNAP_NEW_POINT){k._color="#FF0000";_MAP_WRAPPER.show_snap_point_icon(l[0],l[1],k._snap_type)}else{if(k._snap_type==_SNAP_EXISTING_POINT){k._color="#00FF00";_MAP_WRAPPER.show_snap_point_icon(l[0],l[1],k._snap_type)}}_OP_USER.do_operation(k)}}catch(j){alert("process_ajax_call: "+j)}})}catch(g){alert("_TIMEOUT_FUNCTION layer snap_point: "+g);return false}},300)}}catch(c){alert("Generic_GIS_Utils.snap_point_on_layers(): "+c)}};Generic_GIS_Utils.prototype.log_draw_operation=function(b,c){try{_LIST_OF_OPERATIONS.push(Array(b,c));var a=_GUI_FACTORY.create_gui_command("change_toolbar_status");a._button="toolbar_undo_draw";a._disabled=false;a.execute()}catch(d){alert("Generic_GIS_Utils.log_operation(): "+d)}};Generic_GIS_Utils.prototype.undo_draw_operation=function(){try{if(_LIST_OF_OPERATIONS.length>0){var g=_LIST_OF_OPERATIONS.pop();var k=g[0];var A=g[1];var d=configure_line_path_object();switch(k){case"draw_point":_LIST_OF_POINTS.pop();break;case"draw_line":case"draw_arc":case"draw_circle":d.remove_line(A);break;case"add_vertex":d=d.get_nearest_line_path_to_point(A);d.delete_vertex(A);break;case"add_arc":d=d.get_nearest_line_path_to_point(A);d.delete_vertex(A);break;case"delete_vertex":break;case"polar_line_draw_line":evt_but_polar_line_remove_last(false);break;case"polar_line_remove_last":d._list_of_graphics.push(A);clear_points(_EVENTS_DRAWING_ENGINE);if(d._list_of_graphics.length>0){_LIST_OF_POINTS[0]=A._p2.clone();draw_extra_graphics_object(false,_LIST_OF_POINTS[0],_EVENTS_DRAWING_ENGINE)}break;case"modify_radius":if(_LINE_PATH!=null){d=_LINE_PATH}var q=A[0];if(q instanceof Arc){var v=q._p2;var s=A[1];var z=d.get_line_paths_containing_point(v);for(var u=0;u<z.length;u++){var f=z[u].get_lines_containing_point(v);for(var t=0;t<f.length;t++){var h=f[t];if(h instanceof Arc){if(h._p2.equals(v)){z[u].move_vertex(h._p2,s)}}}}}else{if(q instanceof Circle){var y=A[1];var v=new Point(q._center._x-q._radius,q._center._y);var z=d.get_line_paths_containing_point(v);for(var u=0;u<z.length;u++){var f=z[u].get_lines_containing_point(v);for(var t=0;t<f.length;t++){var h=f[t];if(h instanceof Circle){var l=new Point(h._center._x-h._radius,h._center._y);if(l.equals(v)){h._radius=y}}}}}}break;case"move_vertex":var s=A[0];var v=A[1];if(_LIST_OF_SELECTED_OBJECTS.length>0&&_LIST_OF_SELECTED_OBJECTS[0] instanceof Line_Path&&!_LIST_OF_SELECTED_OBJECTS[0].is_empty()){d=_LIST_OF_SELECTED_OBJECTS[0]}if(_LIST_OF_POINTS.length>0||_LIST_OF_SELECTED_OBJECTS.length>0){for(var u=0;u<_LIST_OF_POINTS.length;u++){if(_LIST_OF_POINTS[u].equals(v)){_LIST_OF_POINTS[u]._x=s._x;_LIST_OF_POINTS[u]._y=s._y;break}}for(u=0;u<_LIST_OF_SELECTED_OBJECTS.length;u++){if(_LIST_OF_SELECTED_OBJECTS[u] instanceof Point&&_LIST_OF_SELECTED_OBJECTS[u].equals(v)){_LIST_OF_SELECTED_OBJECTS[u]._x=s._x;_LIST_OF_SELECTED_OBJECTS[u]._y=s._y;break}}}if(d!=null&&d instanceof Line_Path&&!d.is_empty()){var z=d.get_line_paths_containing_point(v);for(var u=0;u<z.length;u++){var f=z[u].get_lines_containing_point(v);for(var t=0;t<f.length;t++){var h=f[t];if(h instanceof Line){if(h._p1.equals(v)){z[u].move_vertex(h._p1,s)}else{if(h._p2.equals(v)){z[u].move_vertex(h._p2,s)}}}else{if(h instanceof Arc){if(h._p1.equals(v)){z[u].move_vertex(h._p1,s)}else{if(h._p2.equals(v)){z[u].move_vertex(h._p2,s)}else{if(h._p3.equals(v)){z[u].move_vertex(h._p3,s)}}}}else{if(h instanceof Circle){var l=new Point(h._center._x-h._radius,h._center._y);var r=new Point(h._center._x,h._center._y+h._radius);var b=new Point(h._center._x+h._radius,h._center._y);var w=new Point(h._center._x,h._center._y-h._radius);if(l.equals(v)){z[u].move_vertex(l,s)}else{if(r.equals(v)){z[u].move_vertex(r,s)}else{if(b.equals(v)){z[u].move_vertex(b,s)}else{if(w.equals(v)){z[u].move_vertex(w,s)}}}}}}}}}}break;case"remove_ring":var n=A[0];var c=A[1];var a=d.get_nearest_line_path_to_point(n);a.add_graphics(c);break;case"remove_island":d.add_graphics(A);break;case"move_feature":if(_LIST_OF_SELECTED_OBJECTS.length>0){d=_LIST_OF_SELECTED_OBJECTS[0]}d.move(-A[0],-A[1]);if(_LIST_OF_POINTS.length>0){for(var u=0;u<_LIST_OF_POINTS.length;u++){_LIST_OF_POINTS[u].move(-A[0],-A[1])}}break;case"reverse_geometry":if(_LINE_PATH!=null){_LINE_PATH.reverse_geometry()}break}remove_from_extra_graphics(A,_SELECTION_DRAWING_ENGINE);clear_extra_graphics(_EVENTS_DRAWING_ENGINE);draw_extra_graphics(false,_SELECTION_DRAWING_ENGINE)}if(_LIST_OF_OPERATIONS.length==0){var m=_GUI_FACTORY.create_gui_command("change_toolbar_status");m._button="toolbar_undo_draw";m._disabled=true;m.execute()}}catch(x){alert("Generic_GIS_Utils.log_operation(): "+x)}};Generic_GIS_Utils.prototype.remove_draw_operation=function(b,c){try{for(var d=0;d<_LIST_OF_OPERATIONS.length;d++){if(_LIST_OF_OPERATIONS[d][0]==b&&_LIST_OF_OPERATIONS[d][1]==c){_LIST_OF_OPERATIONS.splice(d,1)}}if(_LIST_OF_OPERATIONS.length==0){var a=_GUI_FACTORY.create_gui_command("change_toolbar_status");a._button="toolbar_undo_draw";a._disabled=true;a.execute()}}catch(f){alert("Generic_GIS_Utils.log_operation(): "+f)}};
