我正在使用postgres/postgis和asp.net與c#連接谷歌地圖api v3項目。顯示來自postgis的谷歌地圖數據
我的問題是,我希望能夠顯示空間數據例如:在地圖上的數據庫的興趣點。
是否存在任何將從postgis轉換爲.kml格式的庫?
我正在使用postgres/postgis和asp.net與c#連接谷歌地圖api v3項目。顯示來自postgis的谷歌地圖數據
我的問題是,我希望能夠顯示空間數據例如:在地圖上的數據庫的興趣點。
是否存在任何將從postgis轉換爲.kml格式的庫?
PostGIS'ST_AsKML
函數有一個名爲as_kmldoc的包裝,你可以試一下。它不是很複雜,但可以滿足您的需求。
SELECT as_kmldoc(osgb_location, NAME, code) FROM wunderground_stations;
您可以使用ST_AsKML,但您仍然需要使用該數據創建KML字符串。這裏是完整的功能(在PHP中),它從MySQL數據庫創建世界邊界的KML。
public function generateWorld() {
$tolerance = 20000;
$select = $this->_db->select()
->from (array('w' => 'world_borders'),
array ('cat', 'cntry_name', 'geom' => "ST_AsKML(transform(ST_SimplifyPreserveTopology(transform(w.the_geom, 2249), " . $tolerance . "),4326))"));
$result = $this->_db->fetchAll($select);
$countriesByCat = array();
foreach ($result as $r) {
if ($countriesByCat[$r['cat']] === null) $countriesByCat[$r['cat']] = array();
array_push($countriesByCat[$r['cat']], $r);
}
$kml = '<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">
<Document>
<name>borders</name>';
for($i = 0; $i < count($countriesByCat); $i++) {
$kml .= '<Placemark><name>'.$countriesByCat[$i][0]['cntry_name'].'</name>';
if (count($countriesByCat[$i]) > 1) {
$kml .= '<MultiGeometry>';
foreach ($countriesByCat[$i] as $geom) {
$kml .= $geom['geom'];
}
$kml .= '</MultiGeometry>';
}
else {
$kml .= $countriesByCat[$i][0]['geom'];
}
$kml .= '</Placemark>';
}
$kml .= '</Document></kml>';
echo $kml;
}