在线KML到Google电子表格脚本

时间:2016-09-28 20:08:26

标签: google-apps-script kml

想要将信息用于实时Google地图(KML数据)中的电子表格。下面的代码处理它,但不如所需的快。它必须处理多达10个图层,其中有趣的(参数“限制”)图层(或所有图层)中有大约2000个地标,线位置和/或多边形位置。因此,kml(xml)数据的大小通常非常大。

问题1:如何(如果可能的话)如何实施缓存和/或其他解决方案来处理大约2Mb xml数据?

问题2:如何实现对代码进行流处理的改进,增加动态处理未知(预先)列数/行数的灵活性并降低执行时间?

/**
 * Fetch a Google Map and write the data to the spreadsheet (URL and layer).
 *
 * @param {url} input the URL associated with the Google Map (should contain &forcekml=1).
 * @param {restrict} input the Layer name you want to restrict the data to.
 * @return a range of data depending on the info in the map
 * @customfunction
 */

function IMPORTMAP( url, restrict ) {

  /* One key only allow 100k
  var cache = CacheService.getPublicCache();
  var cached = cache.get( "google-maps-xml" );
  var cached = cache.getAll('?'); //Unknown keys in advance that also may change from time to time.
  if ( cached != null ) {
    return cached;
  }*/
  var txt = UrlFetchApp.fetch( url ).getContentText();
  var sheet = SpreadsheetApp.getActiveSheet();
  var width = sheet.getMaxColumns();
  var height = sheet.getMaxRows();

  var xmlDoc = XmlService.parse( txt );
  var root = xmlDoc.getRootElement();
  var atom = XmlService.getNamespace( 'http://www.opengis.net/kml/2.2' );

  var xml2csv = [[height],[10]];
  var labels = [], label = '', counter = 0, o = 0;
  var documents = root.getChildren( 'Document', atom );
  for( var i = 0; i < documents.length; i++ ) {
    var Folders = documents[i].getChildren( 'Folder', atom );
    for( var j = 0; j < Folders.length && j <= height; j++ ) {
      if( Folders[j].getChild( 'name', atom ).getValue() == restrict ) {
        var Placemarks = Folders[j].getChildren( 'Placemark', atom );
        for( var k = 0; k < Placemarks.length; k++ ) {
          var nodes = Placemarks[k].getChildren();
          for( var l = 0; l < nodes.length; l++ ) {
            var data = nodes[l].getChildren();
            if( data.length > 0 ) {
              for( var m = 0; m < data.length && counter <= width; m++ ) {
                if( data[m].getAttribute( 'name' ) != null ) {
                  if( labels[ data[m].getAttribute( 'name' ).getValue().trim() ] == null ) {
                    labels[ data[m].getAttribute( 'name' ).getValue().trim() ] = counter;
                    xml2csv[ 0 ][ counter++ ] = data[m].getAttribute( 'name' ).getValue().trim();
                  }
                }
                if( data[m].getChild( 'value', atom ) != null ) {
                  xml2csv[ k + 1 ][ labels[ data[m].getAttribute( 'name' ).getValue().trim() ] ] = data[m].getChild( 'value', atom ).getValue().trim();
                } else {
                  o = labels[ data[m].getName().trim() ];
                  if( o == null ) {
                    labels[ data[m].getName().trim() ] = counter;
                    o = counter;
                    xml2csv[ 0 ][ counter++ ] = data[m].getName().trim();
                  }
                  if( xml2csv[ k + 1 ] == null ) {
                    xml2csv[ k + 1 ] = new Array( counter );
                  }
                  xml2csv[ k + 1 ][ labels[ data[m].getName().trim() ] ] = data[m].getValue().trim();
                }
              }
            } else {
              if( label == '' )
                label = nodes[l].getName().trim();
              o = labels[ nodes[l].getName().trim() ];
              if( o == null ) {
                labels[ nodes[l].getName().trim() ] = counter;
                o = counter;
                xml2csv[ 0 ][ counter++ ] = nodes[l].getName().trim();
              }
              if( xml2csv[ k + 1 ] == null ) {
                xml2csv[ k + 1 ] = new Array( counter );
              }
              xml2csv[ k + 1 ][ o ] = nodes[l].getValue().trim();
            }
          }
        }
      }
    }
  }
  //cache.putAll( xml2csv, 1500 ); // cache for 25 minutes
  return xml2csv;
}

请注意,代码仅处理当前形式的特定kml数据,可能不适合更广泛的受众。

0 个答案:

没有答案