[英]styling KML files in c#
我已經在 asp.net(C#) 中編寫了 webapp,它從數據庫中獲取坐標並創建 .KML,然后我將其放入谷歌地圖並工作正常,即繪制線串,但我想將樣式放入其中,即更改地標樣式、顏色,尺寸等。網上沒有關於它的幫助。
代碼:
using SharpKml.Base;
using SharpKml.Dom;
using SharpKml.Engine;
protected void Button1_Click(object sender, EventArgs e)
{
var document = new Document();
document.Id = "null";
document.Name = "null";
LineString linestring = new LineString();
CoordinateCollection coordinates = new CoordinateCollection();
SqlConnection sqlcon = new SqlConnection(conStr);
// String com = "select Latitude, Longitude from Coordinates where IMEI=@txtIMEI";
SqlCommand sqlcom = new SqlCommand("GetLatLon", sqlcon);
sqlcom.CommandType = CommandType.StoredProcedure;
sqlcom.Parameters.Add("@IMEI", SqlDbType.VarChar).Value= TextBox1.Text;
DataTable dt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter(sqlcom);
sda.Fill(dt);
try
{
sqlcon.Open();
sqlcom.ExecuteNonQuery();
foreach (DataRow dr in dt.Rows)
{
double lon = double.Parse(dr["Longitude"].ToString());
double lat = double.Parse(dr["Latitude"].ToString());
coordinates.Add(new Vector(lat, lon));
}
linestring.Coordinates = coordinates;
Placemark placemark = new Placemark();
placemark.Name = "ryan";
placemark.Geometry = linestring;
document.AddFeature(placemark);
var kml = new Kml();
kml.Feature = document;
kml.Feature = placemark;
KmlFile kmlFile = KmlFile.Create(kml, true);
using (var stream = System.IO.File.OpenWrite("C:/"+"IMEI-"+TextBox1.Text+".kml"))
{
kmlFile.Save(stream);
Response.Write("KML Created");
}
}
catch (Exception exc)
{
Response.Write(exc.Message);
}
finally
{
sqlcon.Close();
}
}
}
輸出kml:
<?xml version="1.0" encoding="utf-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Placemark>
<name>ryan</name>
<LineString>
<coordinates>9000,11
71.5460372,34.0000941
71.5460327,34.00009426
71.54603299,34.000094272
71.5460329,34.000094277
71.54603299,34.000094277
71.5460329,34.000094279
71.54603299,34.000094279
71.5460371,34.0000943
71.5460372,34.0000943
71.5460372,34.00009477
71.5460372,34.00009486
71.5460371,34.0000956
71.5460371,34.0000959
71.546037,34.000096
71.546037,34.0000969
71.5460375,34.0000981
71.5460376,34.0000982
71.5460378,34.0000986
71.546038,34.000099</coordinates>
</LineString>
</Placemark>
</kml>
像這樣的改變對我有用
linestring.Coordinates = coordinates;
Placemark placemark = new Placemark();
placemark.Name = "ryan";
placemark.Geometry = linestring;
// Create Style with id
SharpKml.Dom.LineStyle lineStyle = new SharpKml.Dom.LineStyle();
lineStyle.Color = Color32.Parse("ff0000ff");
lineStyle.Width = 2;
Style roadStyle = new Style();
roadStyle.Id = "RoadStyle";
roadStyle.Line = lineStyle;
// Add style to document
document.AddStyle(roadStyle);
// Specify style for your placemark by url
placemark.StyleUrl = new Uri("#RoadStyle", UriKind.Relative);
document.AddFeature(placemark);
如KML-Tutorial示例示例中所述,通過添加高度來生成 YellowLineGreenPoly 樣式進行了修改,這里是代碼片段
var document = new Document();
document.Id = "null";
document.Open = true;
document.Name = "MyDoc";
LineString linestring = new LineString();
linestring.AltitudeMode = AltitudeMode.Absolute;
linestring.Extrude = true;
linestring.Tessellate = true;
CoordinateCollection coordinates = new CoordinateCollection();
foreach (DataRow dr in dt.Rows)
{
double lon = double.Parse(dr["Longitude"].ToString());
double lat = double.Parse(dr["Latitude"].ToString());
double alt = pt.alt.Parse(dr["Altitude"].ToString());;
coordinates.Add(new Vector(lat, lon, alt));
}
linestring.Coordinates = coordinates;
Placemark placemark = new Placemark();
placemark.Name = filename;
placemark.Visibility = false;
placemark.Geometry = linestring;
SharpKml.Dom.LineStyle lineStyle = new SharpKml.Dom.LineStyle();
lineStyle.Color = Color32.Parse("7f00ffff");
lineStyle.Width = 4;
SharpKml.Dom.PolygonStyle PolyStyle = new SharpKml.Dom.PolygonStyle();
PolyStyle.Color = Color32.Parse("7f00ff00");
Style SimpleStyle = new Style();
SimpleStyle.Id = "yellowLineGreenPoly";
SimpleStyle.Line = lineStyle;
SimpleStyle.Polygon = PolyStyle;
document.AddStyle(SimpleStyle);
placemark.StyleUrl = new Uri("#yellowLineGreenPoly", UriKind.Relative);
document.AddFeature(placemark);
var kml = new Kml();
kml.Feature = document;
KmlFile kmlFile = KmlFile.Create(kml, true);
基於前面的示例,我創建了一個具有多種形狀和樣式的快速工作示例。
var document = new Document();
document.Id = "null";
document.Open = true;
document.Name = "MyDoc";
///Style 1
SharpKml.Dom.LineStyle lineStyle = new SharpKml.Dom.LineStyle();
lineStyle.Color = Color32.Parse("FFE67800");
lineStyle.Width = 12;
SharpKml.Dom.PolygonStyle PolyStyle = new SharpKml.Dom.PolygonStyle();
PolyStyle.Color = Color32.Parse("FFE67800");
SharpKml.Dom.Style SimpleStyle = new SharpKml.Dom.Style();
SimpleStyle.Id = "Style1";
SimpleStyle.Line = lineStyle;
SimpleStyle.Polygon = PolyStyle;
document.AddStyle(SimpleStyle);
//Style 2
SharpKml.Dom.LineStyle lineStyle2 = new SharpKml.Dom.LineStyle();
lineStyle2.Color = Color32.Parse("FF1478F0");
lineStyle2.Width = 12;
SharpKml.Dom.PolygonStyle PolyStyle2 = new SharpKml.Dom.PolygonStyle();
PolyStyle2.Color = Color32.Parse("FF1478F0");
SharpKml.Dom.Style SimpleStyle2 = new SharpKml.Dom.Style();
SimpleStyle2.Id = "Style2";
SimpleStyle2.Line = lineStyle2;
SimpleStyle2.Polygon = PolyStyle2;
document.AddStyle(SimpleStyle2);
///
//Drawing shapes
//
List<di_vector> list = new List<di_vector>();
list.Add(new di_vector { longitude = 49.5993894, latitude = 6.1064789 });
list.Add(new di_vector { longitude = 49.5995181, latitude = 6.1064977 });
list.Add(new di_vector { longitude = 49.5994511, latitude = 6.106491 });
list.Add(new di_vector { longitude = 49.5994398, latitude = 6.1066076 });
list.Add(new di_vector { longitude = 49.5995128, latitude = 6.106617 });
list.Add(new di_vector { longitude = 49.599372, latitude = 6.1065969 });
Placemark placemark = new Placemark();
placemark = CreateLineString(list);
placemark.StyleUrl= new Uri("#Style1", UriKind.Relative);
document.AddFeature(placemark);
list.Clear();
list.Add(new di_vector { longitude = 49.5993645, latitude = 6.1066354 });
list.Add(new di_vector { longitude = 49.5995079, latitude = 6.1067373 });
list.Add(new di_vector { longitude = 49.5993523, latitude = 6.1067722 });
list.Add(new di_vector { longitude = 49.5994314, latitude = 6.1067561 });
list.Add(new di_vector { longitude = 49.5994366, latitude = 6.1066877 });
//Placemark placemark = new Placemark();
placemark = CreateLineString(list);
placemark.StyleUrl = new Uri("#Style2", UriKind.Relative);
document.AddFeature(placemark);
var kml = new Kml();
kml.Feature = document;
KmlFile kmlFile = KmlFile.Create(kml, true);
var serializer = new Serializer();
serializer.Serialize(document);
Console.WriteLine(serializer.Xml);
string output = serializer.Xml.Replace("Document", "kml");
創建 LineStrings
/// <summary>
/// Creation of a LineString
/// </summary>
/// <param name="vectors"></param>
/// <param name="_style"></param>
/// <returns></returns>
private Placemark CreateLineString(List<di_vector> vectors)
{
LineString linestring1 = new LineString();
linestring1.AltitudeMode = AltitudeMode.Absolute;
linestring1.Extrude = true;
linestring1.Tessellate = true;
CoordinateCollection coordinates = new CoordinateCollection();
foreach (var item in vectors)
{
coordinates.Add(new SharpKml.Base.Vector(item.longitude,item.latitude));
}
linestring1.Coordinates = coordinates;
Placemark placemark = new Placemark();
placemark.Geometry = linestring1;
return placemark;
}
向量類
/// <summary>
/// vector class
/// </summary>
public class di_vector
{
public double longitude { get; set; }
public double latitude { get; set; }
}
生成的輸出 KML
<? xml version="1.0" encoding="utf-8"?>
<kml id = "null" xmlns="http://www.opengis.net/kml/2.2">
<name>MyDoc</name>
<open>true</open>
<Style id = "Style1" >
< LineStyle >
< color > 501400dc</color>
<width>12</width>
</LineStyle>
<PolyStyle>
<color>501400dc</color>
</PolyStyle>
</Style>
<Style id = "Style2" >
< LineStyle >
< color > 5014f01e</color>
<width>12</width>
</LineStyle>
<PolyStyle>
<color>501400dc</color>
</PolyStyle>
</Style>
<Placemark>
<styleUrl>#Style1</styleUrl>
<LineString>
<extrude>true</extrude>
<tessellate>true</tessellate>
<altitudeMode>absolute</altitudeMode>
<coordinates>6.1064788999999999,49.5993894
6.1064977000000003,49.599518099999997
6.1064910000000001,49.599451100000003
6.1066076000000002,49.599439799999999
6.106617,49.599512799999999
6.1065969000000004,49.599372000000002</coordinates>
</LineString>
</Placemark>
<Placemark>
<styleUrl>#Style2</styleUrl>
<LineString>
<extrude>true</extrude>
<tessellate>true</tessellate>
<altitudeMode>absolute</altitudeMode>
<coordinates>6.1066354,49.5993645
6.1067372999999998,49.599507899999999
6.1067722,49.5993523
6.1067561000000001,49.5994314
6.1066877000000002,49.599436599999997</coordinates>
</LineString>
</Placemark>
</kml>
首先,看看我下面的例子:
1.創建一個<Document>節點
2.添加與<Placemark>節點同級的<Style>節點
3.在<Placemark>內的<styleUrl>節點中引用<Style>節點id屬性(id = YellowLineGreenPoly)
注意:測試 KML 文件的一個有用的地方是 - http://display-kml.appspot.com/
<?xml version="1.0" encoding="utf-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
<Style id="yellowLineGreenPoly">
<LineStyle>
<color>7f00ffff</color>
<width>4</width>
</LineStyle>
<PolyStyle>
<color>7f00ff00</color>
</PolyStyle>
<IconStyle>
<Icon>
<href>http://maps.google.com/mapfiles/kml/paddle/red-stars.png</href>
</Icon>
</IconStyle>
</Style>
<Placemark>
<name>stu</name>
<styleUrl>#yellowLineGreenPoly</styleUrl>
<Point>
<coordinates>71.5460372,34.0000941</coordinates>
</Point>
</Placemark>
<Placemark>
<name>ryan</name>
<styleUrl>#yellowLineGreenPoly</styleUrl>
<LineString>
<coordinates>9000,11
71.5460372,34.0000941
71.5460327,34.00009426
71.54603299,34.000094272
71.5460329,34.000094277
71.54603299,34.000094277
71.5460329,34.000094279
71.54603299,34.000094279
71.5460371,34.0000943
71.5460372,34.0000943
71.5460372,34.00009477
71.5460372,34.00009486
71.5460371,34.0000956
71.5460371,34.0000959
71.546037,34.000096
71.546037,34.0000969
71.5460375,34.0000981
71.5460376,34.0000982
71.5460378,34.0000986
71.546038,34.000099</coordinates>
</LineString>
</Placemark>
</Document>
</kml>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.