Example #1
0
        public XmlDocument getKML(int connID)
        {
            String serverPath = "http://" + HttpContext.Current.Request.ServerVariables["SERVER_NAME"] + ":"
                + HttpContext.Current.Request.ServerVariables["SERVER_PORT"];

            //create new connection a populate fields to get the connection name for KMLGenerator
            Connection conn = new Connection(connID);
            conn.populateFields();
            string name = conn.getConnInfo().getConnectionName();

            //create a new kml genereator with the connection name as the placemark name
            KMLGenerator kmlGen = new KMLGenerator(name, serverPath);

            string kml = "";
            //generate the kml for the given connID
            try
            {
                kml = kmlGen.generateKML(connID);
            }
            catch (Exception e)
            {
                //if there was an error generating kml, return a kml file that contains only a screen overlay that states there was an error generating kml
                kml = "<ScreenOverlay>	<name>KML Error</name>	<Icon>		<href>" + serverPath + "/graphics/kml-error.png</href>	</Icon>	<overlayXY x=\"0.5\" y=\"0.5\" xunits=\"fraction\" yunits=\"fraction\"/>	<screenXY x=\"0.5\" y=\"0.5\" xunits=\"fraction\" yunits=\"fraction\"/>	<rotationXY x=\"0\" y=\"0\" xunits=\"fraction\" yunits=\"fraction\"/>	<size x=\"1\" y=\"0.2\" xunits=\"fraction\" yunits=\"fraction\"/></ScreenOverlay>";
            }
            //add the kml to an XMLDoc and return
            XmlDocument kmlDoc = new XmlDocument();
            kmlDoc.LoadXml(kml);
            return kmlDoc;
        }
Example #2
0
        protected void genKMLFunction(object sender, EventArgs e)
        {
            try
            {
                //Generate the KML from the connection
                ImageButton sendBtn = (ImageButton)sender;
                String serverPath = "http://" + Request.ServerVariables["SERVER_NAME"] + ":" + Request.ServerVariables["SERVER_PORT"];
                string args = sendBtn.CommandArgument.ToString();
                KMLGenerator kml = new KMLGenerator(ConnInfo.getConnInfo(Convert.ToInt32(args)).getConnectionName(), serverPath);

                //Generate the KML string based on the connection id
                String kmlString = kml.generateKML(int.Parse(args));
                Connection conn = new Connection(int.Parse(args));
                conn.populateFields();

                //Write the KML string to a downloadable file
                Response.ClearHeaders();
                Response.ClearContent();
                Response.ContentType = "application/vnd.google-earth.kml+xml kml";
                Response.AddHeader("Content-Disposition", "attachment; filename=\"" + (conn.getConnInfo()).getConnectionName() + ".kml");
                Response.Write(kmlString);
                Response.End();
                return;
            }
            catch (ODBC2KMLException ex)
            {
                ErrorHandler err = new ErrorHandler(ex.errorText, errorPanel1);
                err.displayError();
                return;
            }

            //Response.Redirect("Main.aspx", true);
        }
Example #3
0
        /// <summary>
        /// This function takes a connection object and generates all the associated KML for that connection.
        /// It works hand-in-hand with KMLGenerationLibrary, Styles, and Placemarks.
        /// 
        /// There is also a helper class used to prevent duplicate styles (HashStyleComparer).
        /// </summary>
        /// <param name="connection">Connection --> connection object for the connection that you wish to generate KML for</param>
        /// <returns>String --> A string that is the KML</returns>
        public string generateKMLFromConnection(Connection connection)
        {
            //if the mapping is invalid, throw an exception and don't try to continue
            if (!connection.mapping.isValid(connection.connInfo))
            {
                throw new ODBC2KMLException("There was an exception generating KML. Mapping is invalid.");
            }

            //Needed to generate KML, parameter is desired file name within KML file
            KMLGenerationLibrary kmlGenerator = new KMLGenerationLibrary(this.fileName);

            try
            {
                //Get mappings fromc onnection object
                Mapping map = connection.getMapping();

                //Create array list to hold places
                ArrayList placemarks = new ArrayList();
                //Create hashset to hold unique styles, takes HashStyleComparer which is a helper class
                HashSet<Style> styles = new HashSet<Style>(new HashStyleComparer());

                //Create the Icon array and grabs the icons for the connection
                ArrayList icons = new ArrayList();
                icons = connection.getIcons();

                //Create the overlay array and grab the overlays for the connection
                ArrayList overlays = new ArrayList();
                overlays = connection.getOverlays();

                //Retrieve description string
                String descString = connection.getDescription().getDesc();

                //Create an array to store new description values
                ArrayList descArray = new ArrayList();

                //Create data table to pass to parser
                DataTable remote = null;

                //Create database
                Database DB = new Database(connection.getConnInfo());

                //Grab the tablename out of mapping
                String tableName = map.getTableName();

                try
                {
                    if (connection.getConnInfo().getDatabaseType() == ConnInfo.MSSQL)
                    {
                        remote = DB.executeQueryRemote("SELECT * FROM " + tableName);
                    }
                    else if (connection.getConnInfo().getDatabaseType() == ConnInfo.MYSQL)
                    {
                        remote = DB.executeQueryRemote("SELECT * FROM " + tableName + ";");
                    }
                    else if (connection.getConnInfo().getDatabaseType() == ConnInfo.ORACLE)
                    {
                        remote = DB.executeQueryRemote("SELECT * FROM \"" + tableName + "\"");
                    }
                }
                catch
                {
                    throw new ODBC2KMLException("There was an exception generating KML. There was a problem retreiving data from the remote server");
                }

                //Parsed descriptions for rows
                descArray = Description.parseDesc(remote, descString, tableName);

                int counter = 0;
                //For each row in the table!!!
                foreach (DataRow remoteRow in remote.Rows)
                {
                    //Set placemark name
                    string placemarkName;
                    try
                    {
                        if (map.getPlacemarkFieldName() == null || map.getPlacemarkFieldName().Trim().Equals("") || map.getPlacemarkFieldName().Trim().Equals("No placemark name mapped"))
                        {
                            placemarkName = "";
                        }
                        else
                        {
                            placemarkName = (String)remoteRow[map.getPlacemarkFieldName()];

                        }
                    }
                    catch
                    {
                        throw new ODBC2KMLException("There was an exception generating KML. Error parsing placemark.");
                    }

                    //Foreach row set the description for each row
                    String rowDesc = descArray[counter].ToString();

                    //Declare the lat and long holders
                    Double rowLat = 0, rowLon = 0;

                    //Check to see how many columns there are
                    try
                    {
                        if (map.getFormat() != Mapping.SEPARATE)
                        {
                            //Select the column value
                            String column = "";
                            foreach (DataColumn remoteColumn in remote.Columns)
                            {
                                if (remoteColumn.ColumnName == map.getLatFieldName())
                                {
                                    column = remoteRow[remoteColumn].ToString();
                                }
                            }

                            //Create the array to hold the coordinates
                            double[] coordinates;

                            //Separate the coordinates
                            //Order == Latitude First
                            if (map.getFormat() == Mapping.LATFIRST)
                            {
                                coordinates = map.separate(column, Mapping.LATFIRST);
                                rowLat = coordinates[0];
                                rowLon = coordinates[1];
                            }
                            else //Order == Longitude first
                            {
                                coordinates = map.separate(column, Mapping.LONGFIRST);
                                rowLon = coordinates[0];
                                rowLat = coordinates[1];
                            }
                        }
                        else//Two separate columns
                        {
                            //Get coordinates
                            foreach (DataColumn remoteColumn in remote.Columns)
                            {
                                if (remoteColumn.ColumnName == map.getLatFieldName())
                                {
                                    rowLat = Double.Parse(remoteRow[remoteColumn].ToString());
                                }
                                else if (remoteColumn.ColumnName == map.getLongFieldName())
                                {
                                    rowLon = Double.Parse(remoteRow[remoteColumn].ToString());
                                }
                            }//End for each
                        }//End else
                    }
                    catch
                    {
                        throw new ODBC2KMLException("There was an exception generating KML. Error parsing lat/long rows.");
                    }

                    //Row's icon
                    Icon rowIcon = new Icon();
                    rowIcon.setLocation("");

                    //For each icon until the first one found, compare the icons
                    //conditions against the given row
                    Boolean breakLoop = false;
                    foreach (Icon i in icons)
                    {
                        foreach (Condition c in i.getConditions())
                        {
                            //See if the condition applies to the given row
                            if (c.evaluateCondition(remoteRow, c, tableName))
                            {
                                //Set temp icon to row icon and tell it to break out
                                rowIcon = new Icon(i);
                                breakLoop = true;
                            }

                            //Grabbed the first icon, break out
                            if (breakLoop)
                                break;
                        }//End inner for each

                        //Grabbed the first icon, break out
                        if (breakLoop)
                            break;
                    }//End outer for each

                    //Long unsigned int, needed to properly interpret colors
                    UInt64 color = 0;
                    foreach (Overlay o in overlays)
                    {
                        foreach (Condition c in o.getConditions())
                        {
                            //See if the condition applies
                            if (c.evaluateCondition(remoteRow, c, tableName))
                            {
                                if (color == 0)
                                {
                                    //Set the color to hex value
                                    color = 0xFF000000;
                                }
                                //Mix the colors, if multiple colors work
                                color = color | (Convert.ToUInt64(o.getColor(), 16));
                            }
                        }//End inner for each
                    }//End outer for each

                    //Create Style and placemark for this coordinate set
                    Style rowStyle = new Style();
                    Placemark rowPlacemark;

                    //if there is an icon, create the name of the style based on the icon name and color
                    if (rowIcon.getLocation() != "")
                    {
                        if (rowIcon.getLocality() == false)
                        {
                            //Create new style with external icon
                            rowStyle = new Style(rowIcon, color, (rowIcon.getLocation() + "_" + color.ToString("X")));
                            rowIcon.setLocation("");
                        }
                        else //If the icon is local, append server data
                        {
                            //Create the new style, with local icon
                            rowIcon.setLocation(this.serverPath + rowIcon.getLocation());
                            rowStyle = new Style(rowIcon, color, (rowIcon.getLocation() + "_" + color.ToString("X")));
                            rowIcon.setLocation("");
                        }
                    }
                    else if (rowIcon.getLocation() == "" && color != 0) //Create the style name based on the color
                    {
                        rowStyle = new Style(rowIcon, color, color.ToString("X"));
                    }
                    else //If rowstyle is null, ignore it
                    {
                        rowStyle = null;
                    }

                    //Create placemark and add it to array list
                    rowPlacemark = new Placemark(rowLat, rowLon, rowDesc, placemarkName);

                    placemarks.Add(rowPlacemark);

                    //If there is a row style, add it to the placemark and the array list
                    if (rowStyle != null)
                    {
                        rowPlacemark.setPlacemarkStyleName("#" + rowStyle.getStyleName());
                        styles.Add(rowStyle);
                    }
                    else
                    {
                        //Default value which won't add a style to this placemark in KML
                        rowPlacemark.setPlacemarkStyleName("");
                    }

                    //Increment counter for next row (associated with getting the row description)
                    counter++;

                }//End for each
                // }//End for each

                //Add each style to the KML
                foreach (Style s in styles)
                {
                    kmlGenerator.addStyle(s);
                }

                //Used to check if a look at has been added
                Boolean addLookAt = false;

                //Add each placemark to the KML
                foreach (Placemark p in placemarks)
                {
                    kmlGenerator.addPlacemark(p);
                    if (!addLookAt) //Add the first placemark as default lookat
                    {
                        kmlGenerator.addLookAt(p);
                        addLookAt = true;
                    }
                }
            }
            catch (ODBC2KMLException e) //If bad things happen pass it up to connection details
            {
                throw e;
            }

            //Return KML string
            return kmlGenerator.finalizeKML();
        }