2012-04-03 16 views
14

से Google मानचित्र पर बहुभुज प्रदर्शित करना मेरे पास एक SQL सर्वर 2008 डेटाबेस है जिसमें विभिन्न ऑस्ट्रेलियाई क्षेत्रों के आकार को संग्रहीत करने वाले प्रकार भूगोल के कॉलम के साथ एक SQL Server 2008 डेटाबेस है। मैं इन आकृतियों को Google मानचित्र पर आकर्षित करने में सक्षम होना चाहता हूं।SQL सर्वर भूगोल डेटा प्रकार

यह एएसपी.NET सी # वेबसाइट के लिए है।

मैंने किसी भी नमूने की खोज की है कि यह कैसे करें लेकिन कुछ भी नहीं मिला?

क्या किसी के पास ऐसा करने के कुछ नमूने हैं, विशेष रूप से SQL सर्वर से भूगोल डेटा का उपयोग करते हुए?

उत्तर

11

एडमब्लू का उत्तर सही है, हालांकि एसक्यूजीजी डेटा प्रारूप में मौजूद डेटा को संबोधित नहीं करता है।

Microsoft.SqlServer.Types के लिए एक संदर्भ शामिल

SqlCommand cmd = new SqlCommand("SELECT STATEMENT",ConnectionString); 
connectionString.Open(); 
SqlDataReader polygon = cmd.ExecuteReader(); 

While (polygon.read()) 
{ 
    string kmlCoordinates = string.Empty; 
    SqlGeography geo = (SqlGeography)polygon["GeoColumn"]; 
    for(int i = 1; i <= geo.STNumPoints(); i++) 
    { 
     SqlGeography point = geo.STPointN(i); 
     kmlCoordinates += point.Long + "," + point.Lat + " "; 
    } 
{ 

ConnectionString.Close(); 

नोट: भूगोल अंक 1 इंडेक्स किए गए नहीं 0 अनुक्रमित, और यह या तो foreach अनुकूल नहीं है।

+0

मुझे एहसास है कि यह थोड़ा नाइट-पिक्य है - लेकिन आपको इन तरह के उत्तरों में बयान के उपयोग के उचित उपयोग को स्पष्ट करना चाहिए। विशेष रूप से इस उपयोग के मामले में, मुझे लगता है कि आपकी वस्तुओं का सही ढंग से निपटान नहीं करना कुछ महत्वपूर्ण स्मृति रिसाव का कारण बन सकता है। – Wjdavis5

7

मैंने वेबपृष्ठों पर बहुभुज को ओवरले करने के लिए अतीत में केएमएल फ़ाइलों का उपयोग किया है।

मैं पढ़ने googles KML tutorials

  • अपने डेटाबेस से पढ़ने का
  • KML Google एपीआई से फ़ाइल
  • कॉल KML फ़ाइल कैसे बना एक समारोह बनाएं सुझाव है

एम एल के साथ आप प्रदान करते हुए आकार को ओवरले करने का एक तेज़ और आसान तरीका, Google प्रदर्शित वस्तुओं की संख्या पर एक सीमा डालता है।

नीचे आपको केएमएल दृष्टिकोण के साथ शुरू करने में मदद करनी चाहिए। Math.random (के अलावा तो

public ActionResult Kml() 
    { 
     DataAccess da = new DataAccess(); 
     string cellColor = "0032FB"; 

     string kml = @"<?xml version=""1.0"" encoding=""UTF-8""?> 
     <kml xmlns=""http://earth.google.com/kml/2.1""> 
      <Document> 
       <Style id="polygon"> 
        <LineStyle> 
         <color>FF" + cellColor + @"</color> 
        </LineStyle> 
        <PolyStyle> 
         <color>44" + cellColor [email protected]"</color> 
         <fill>1</fill> 
         <outline>1</outline> 
        </PolyStyle> 
       </Style> 
       <name>some name</name> 
       <description>some des</description> 

     "; 
     DataTable polygons; 

     foreach (DataRow polygon in polygons.Rows) 
     { 
       kml += @" 
        <Placemark> 
         <name>"somename @"</name> 
         <description><![CDATA[<p>some text</p>]]></description>" + 
         @"<styleUrl>#polygon</styleUrl> 
         <Polygon> 
          <extrude>1</extrude> 
          <altitudeMode>clampToSeaFloor</altitudeMode> 
          <outerBoundaryIs> 
           <LinearRing> 
            <coordinates>" + 
             polygon["Cell Limit Longitude West"].ToString() + "," + polygon["Cell Limit Latitude North"].ToString() + " " + 
             polygon["Cell Limit Longitude East"].ToString() + "," + polygon["Cell Limit Latitude North"].ToString() + " " + 
             polygon["Cell Limit Longitude East"].ToString() + "," + polygon["Cell Limit Latitude South "].ToString() + " " + 
             polygon["Cell Limit Longitude West"].ToString() + "," + polygon["Cell Limit Latitude South "].ToString() + " " + 
             polygon["Cell Limit Longitude West"].ToString() + "," + polygon["Cell Limit Latitude North"].ToString() + " " + 
            @"</coordinates> 
           </LinearRing> 
          </outerBoundaryIs> 
         </Polygon> 
        </Placemark> 
       "; 
     } 

     kml += @"</Document> 
     </kml>"; 
     byte[] data = Encoding.ASCII.GetBytes(kml); 

     return File(data, "application/vnd.google-earth.kml+xml", id); 
    } 

जावास्क्रिप्ट

var url = 'http://www.example.com/AppName/GMap/file.kml &rand=' + Math.random(); 

layer_paperCharts = new google.maps.KmlLayer(url); 

if (loadedonce) { 
    layer_paperCharts.set('preserveViewport', true); 
} else { 
    loadedonce = true; 
} 

layer_paperCharts.setMap(map); 

गूगल कैश KML फ़ाइलों) इस के आसपास मिल जाएगा।

आप Fusion Tables पर भी देख सकते हैं। हालांकि आपको अपना डेटा Google पर अपलोड करना होगा। Google समूह प्रस्तुत डेटा भी। लेकिन आप एसक्यूएल चाहते हैं तो यह विकल्प आपके लिए उपलब्ध नहीं हो सकता है।

6
public void KmlExport() 
    { 
     string cellColor = "COLOR"; 
     string KMLname = "KML NAME"; 
     string description = "KML DESCRIPTION"; 
     string kml = @"<?xml version=""1.0"" encoding=""UTF-8""?> 
         <kml xmlns=""http://www.opengis.net/kml/2.2""> 
          <Document> 
           <Style id=""polygon""> 
            <LineStyle> 
             <color>FF" + cellColor + @"</color> 
            </LineStyle> 
            <PolyStyle> 
             <color>44" + cellColor + @"</color> 
             <fill>1</fill> 
             <outline>1</outline> 
            </PolyStyle> 
           </Style> 
           <name>" + KMLname + @"</name> 
           <description>" + description + "</description>"; 

     SqlCommand cmd = new SqlCommand("Select Statement", connectionString); 
     cs.Open(); 
     SqlDataReader polygon = cmd.ExecuteReader(); 

     while (polygon.Read()) 
     { 
      string kmlCoordinates = string.Empty; 
      SqlGeography geo = (SqlGeography)polygon["GEOGRAPHY COLUMN"]; 

       for (int i = 1; i <= geo.STNumPoints(); i++) 
       { 
        SqlGeography point = geo.STPointN(i); 
        kmlCoordinates += point.Long + "," + point.Lat + " "; 
       } 

       string polyName = polygon["Name Column"].ToString(); 
       string polyDescription = polygon["Description Column"].ToString(); 
       kml += @" 
       <Placemark> 
        <name>" + polyName + @"</name> 
        <description><![CDATA[<p>" + polyDescription + "</p>]]></description>" + 
         @"<styleUrl>#polygon</styleUrl> 
        <Polygon> 
         <extrude>1</extrude> 
         <altitudeMode>clampToSeaFloor</altitudeMode> 
         <outerBoundaryIs> 
          <LinearRing> 
           <coordinates>" + kmlCoordinates + 
           @"</coordinates> 
          </LinearRing> 
         </outerBoundaryIs> 
        </Polygon> 
       </Placemark>"; 

       kmlCoordinates = string.Empty; 
      } 

     cs.Close(); 
     kml += @"</Document></kml>"; 
     StreamWriter file = new StreamWriter(@"OUTPUTFILE.KML"); 
     file.WriteLine(kml); 
     file.Close(); 

यह एडम डब्ल्यू और ब्लेयर एम के समाधान का एक संयोजन है। मैंने इसे संशोधित किया ताकि आपके डेटाबेस में एक से अधिक पॉलीगॉन होने पर केएमएल फ़ाइल जेनरेट की जाएगी।

संबंधित मुद्दे