/// <summary> /// Adds a default position for the KML file to view. /// It looks at the first placemark added. /// </summary> /// <param name="place">Placemark --> desired look at placemark</param> public void addLookAt(Placemark place) { formattedKML += "\t<LookAt>" + "\t\t<longitude>" + place.getPlacemarkLongitude() + "</longitude>" + "<latitude>" + place.getPlacemarkLatitude() + "</latitude>" + "<heading>0</heading> " + "<range>3000000</range>" + "<altitudeMode>relativeToGround</altitudeMode>" + "\t</LookAt>"; }
/// <summary> /// Adds a placemark to the KML file and associates the placemark with /// latitude and longitude coordinates, a description, a name, and a desired style /// </summary> /// <param name="name">String --> placemark name</param> /// <param name="description">String --> placemark description</param> /// <param name="lat">double --> Latitude</param> /// <param name="lon">double --> Longitude</param> /// <param name="styleName">String --> Style Name</param> public void addPlacemark(Placemark place) { formattedKML += "\t<Placemark>\n" + "\t\t<name>" + "\t\t\t<![CDATA[" + place.getPlacemarkName() + "]]>\n" + "</name>\n" + "\t\t<description>\n" + "\t\t\t<![CDATA[" + place.getPlacemarkDescription() + "]]>\n" + "\t\t</description>\n"; if (place.getPlacemarkStyleName().Length != 0) { formattedKML += "\t\t\t<styleUrl>" + place.getPlacemarkStyleName() + "</styleUrl>\n"; } formattedKML += "\t\t\t<Point>\n" + "\t\t\t\t<altitudeMode>relativeToGround</altitudeMode>\n" + "\t\t\t\t<coordinates>" + place.getPlacemarkLongitude() + "," + place.getPlacemarkLatitude() + "</coordinates>\n" + "\t\t\t</Point>\n" + "\t</Placemark>\n"; }
/// <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()); }//End function
/// <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(); }