2012-04-09 117 views
3

我有一些形狀數據(熔合表和/或數據庫列中的一系列kml文件),我想將它與另一個包含緯度經度的表合併點。基本上我想要一些方法來確定一個給定的經緯度點是否包含在kml形狀的內部,如果是這樣,保存對該行的參考。 我雖然也許有一種方法可以在融合表內做到這一點,但如果沒有,也許有一種方法可以循環每個kml並測試是否包含經緯度點。我理解這並不是非常有效。 任何幫助,算法,服務等都會​​很棒。如何確定一個點是否位於kml或shape內

回答

3

Fusion Tables SQL API具有ST_INTERSECTS運算符,但只能找到CIRCLE或RECTANGLE中的點。 GMap V3有一個幾何庫,它有一個poly.containsLocation()方法,我認爲它可以用於任意多邊形。參見:GoogleMap geometry/poly library

P.S.我知道這不適合KML文件的工作,但他們確實含有可以變成GMAP多邊形

+0

豔光四射,這是一個很好的開始 – user379468 2012-04-13 14:56:54

0

這可能是長久以來解決了多邊形點,但我也有類似的問題,我想我會後我的解決辦法。

我有兩個Fusion表i)地址&數據和ii)多邊形&數據。我希望能夠輕鬆地查詢以查找一個或多個多邊形內的所有地址。我可以通過我的映射網頁實時執行此操作,但決定最好事先查找相關的多邊形,然後使用此數據執行我的映射查詢(在Web界面中更快更輕鬆地指定多個多邊形)。

因此,我將我的表格i)導出到Google表單的地址中,並創建了一個簡短的腳本,用於檢查地址所在的多邊形並將其寫回到Google表單。然後我更新了我的融合表我)。

我有一個近3000個地址和2000個多邊形的數據集,所以它超時了幾次,並且有幾個地址錯誤,但這很容易解決,我只是將腳本設置爲從第一行開始運行尚未更新。請注意,如果這些多邊形彼此重疊,但這不會起作用,因爲它們是地理邊界;-)

我使用的代碼在下面,也在要點here上。您顯然需要更新表ID,並且可能需要使用OAuth(我不太瞭解,但是遵循Google的說明並完成)做了一些事情。

// replace with your fusion table's id (from File > About this table) 
 
var TABLE_ID = 'xxxxxxxxxx'; 
 

 
// first row that has data, as opposed to header information 
 
var FIRST_DATA_ROW = 2; 
 
var FIRST_DATA_COLUMN = 11; 
 
var LAT_COLUMN = 1; 
 
var LNG_COLUMN = 2; 
 
var SA2_COLUMN = 6; 
 
var SA3_COLUMN = 7; 
 

 
/** 
 
* Uses a lat and lng data in google sheets to check if an address is within a kml polygon 
 
* in a list of KML polygons in fusion (in this case ABS/ASGC SA2 and SA3 regions, but could be any polygon) 
 
* the function then stores the ID/name of the relevant polygon in google sheets 
 
* I could check this data in realtime as I render maps, but as it doesn't changed, figure its better to just record 
 
* which polygon each address pertains to so its quicker and easier to search (in particular it mades it easier to write a query 
 
* which identifies all the address within multiple polygons) 
 
* in this case I had 3000 rows so it exceeded maximum execution times, so I just updated the first data row a couple of times 
 
* when the execution time exceeded. 
 
*/ 
 
function updateSA2ID() { 
 
    var tasks = FusionTables.Task.list(TABLE_ID); 
 
    var sqlResponse = ''; 
 
    
 
    // Only run if there are no outstanding deletions or schema changes. 
 
    if (tasks.totalItems === 0) { 
 
    var sheet = SpreadsheetApp.getActiveSheet(); 
 
    var latLngData = sheet.getRange(FIRST_DATA_ROW, FIRST_DATA_COLUMN, sheet.getLastRow(), sheet.getLastColumn()); 
 
    
 
     i = 1; 
 
    // Loop through the current current sheet 
 
    for (i = 1; i <= latLngData.getNumRows(); i++) {  
 
     
 
     // cross reference to Fusion table 
 
     lat = latLngData.getCell(i,LAT_COLUMN).getValue(); 
 
     lng = latLngData.getCell(i,LNG_COLUMN).getValue(); 
 
      
 
     sqlString = "SELECT 'SA2 Code', 'SA3 Code' FROM " + TABLE_ID + " WHERE ST_INTERSECTS(geometry, CIRCLE(LATLNG(" + lat + ", " + lng + "),1)) ";  
 
     //Browser.msgBox('Lat ' + lat + ' Lng ' + lng + '; ' + sqlString, Browser.Buttons.OK); 
 
     sqlResponse = FusionTables.Query.sql(sqlString); 
 
     //Browser.msgBox('SQL Response ' + sqlResponse, Browser.Buttons.OK); 
 

 
     latLngData.getCell(i,SA2_COLUMN).setValue(sqlResponse.rows[0][0]); // set SA2 
 
     latLngData.getCell(i,SA3_COLUMN).setValue(sqlResponse.rows[0][1]); // set SA3  
 

 
    } 
 
     
 
    } 
 
    else { 
 
    Logger.log('Skipping row replacement because of ' + tasks.totalItems + ' active background task(s)'); 
 
    } 
 
};

相關問題