コード例 #1
0
ファイル: index.js プロジェクト: JBurant/node-osmium
child.on('exit', function(code, signal) {

    var db = new sqlite3.Database(output_filename);
    db.serialize();

    db.spatialite();
    db.run("PRAGMA synchronous = OFF;"); // otherwise it is very slow

    // we don't need this here because of the workaround above
    //db.run("SELECT InitSpatialMetaData('WGS84');");

    db.run("CREATE TABLE amenities (osm_id INT, name TEXT);");
    db.run("SELECT AddGeometryColumn('amenities', 'geom', 4326, 'POINT', 2);");

    db.run("CREATE TABLE highways (osm_id INT, htype TEXT, name TEXT);");
    db.run("SELECT AddGeometryColumn('highways', 'geom', 4326, 'LINESTRING', 2);");

    var add_amenity = db.prepare("INSERT INTO amenities (osm_id, name, geom) VALUES (?, ?, GeomFromWKB(?, 4326))");
    var add_highway = db.prepare("INSERT INTO highways (osm_id, htype, name, geom) VALUES (?, ?, ?, GeomFromWKB(?, 4326))");

    var handler = new osmium.Handler();

    handler.on('node', function(node) {
        if (node.tags('amenity') == 'school') {
            add_amenity.run(node.id, node.tags('name'), node.wkb());
        }
    });

    handler.on('way', function(way) {
        if (way.tags('highway')) {
            if (way.wkt) {
                add_highway.run(way.id, way.tags('highway'), way.tags('name'), way.wkb());
            }
        }
    });

    handler.on('done', function() {
        add_highway.finalize();
        add_amenity.finalize();
        db.close();
    });

    var location_handler = new osmium.LocationHandler();
    var reader = new osmium.BasicReader(input_filename);
    osmium.apply(reader, location_handler, handler);
    reader.close();

});
コード例 #2
0
ファイル: test2.js プロジェクト: psm21-vagrant/test
db.spatialite(function(err) {
  db.each(sql, function(err, row) {
    
	var obj = JSON.parse(row.geometry);
	console.log(obj.coordinates);
  });
});
コード例 #3
0
ファイル: spatialite.js プロジェクト: psm21-vagrant/test
	db.spatialite(function(err) {
		db.all(sql, function(err, rows) {
			if ( rows != undefined ){
				if ( rows != null ){
					for ( var i = 0; i < rows.length; i++ ){
						nodes.push(rows[i]);
					}			
				}
			}
			n = nodes.length;
			callback();
		});
	});
コード例 #4
0
ファイル: spatialite.js プロジェクト: psm21-vagrant/test
	db.spatialite(function(err) {
		db.all(sql, function(err, rows) {
			results = [];
			if ( rows != undefined ){
				if ( rows != null ){
					for ( var i = 0; i < rows.length; i++ ){
						results.push(rows[i]);
					}			
				}
			}
			callback(results);
		});
	});
コード例 #5
0
ファイル: spatialite.js プロジェクト: psm21-vagrant/test
	db.spatialite(function(err) {
		db.get(sql, function(err, row) {
            //console.log(JSON.stringify(row));
            route = [];
			if ( row != undefined ){
				if ( row.geometry != null ){
					var obj = JSON.parse(row.geometry);
					route = obj.coordinates;
				}
			}
			//console.log(JSON.stringify(reverse(route)));
            callback(reverse(route));
		});
	});
コード例 #6
0
ファイル: spatialite.js プロジェクト: psm21-vagrant/test
/**
* получение узлов графа из базы в виде массива объектов и запись в массив nodes
* @param callback функция обратного вызова
* nodes - массив объектов вида {node_id:node_id,cardinality:cardinality,lat:lat,lng:lng}
**/
function loadNodes(callback){
	var sql = "SELECT node_id, cardinality, Y(geometry) AS lat, X(geometry) AS lng FROM roads_nodes"; 
	db.spatialite(function(err) {
		db.all(sql, function(err, rows) {
			if ( rows != undefined ){
				if ( rows != null ){
					for ( var i = 0; i < rows.length; i++ ){
						nodes.push(rows[i]);
					}			
				}
			}
			n = nodes.length;
			callback();
		});
	});
}
コード例 #7
0
ファイル: spatialite.js プロジェクト: psm21-vagrant/test
/**
* получение дорог из базы в виде массива объектов и запись в массив roads + заполнение индексных массивов
* @param callback функция обратного вызова
* roads - массив объектов вида {node_from:node_from,node_to:node_to,name:name,cost:cost,length:length,lat_from:lat_from,lng_from:lng_from,lat_to:lat_to,lng_to:lng_to}
**/
function loadRoads(callback){
	var sql = "SELECT node_from, node_to, name, cost, length, Y(rn.geometry) "; 
		sql += "AS lat_from, X(rn.geometry) AS lng_from, Y(rn2.geometry) "; 
		sql += "AS lat_to, X(rn2.geometry) AS lng_to, AsGeoJSON(r.geometry) AS geometry ";  
		sql += "FROM roads r,roads_nodes rn, roads_nodes rn2 "; 
		sql += "WHERE r.node_from=rn.node_id AND r.node_to=rn2.node_id ORDER BY node_from,node_to";
	
	
	db.spatialite(function(err) {
		db.all(sql, function(err, rows) {
			if ( rows != undefined ){
				if ( rows != null ){
					//записываем в массив
					var geom = null;
					for ( var i = 0; i < rows.length; i++ ){
						roads.push(rows[i]);
						geom = JSON.parse(rows[i].geometry);
						geom.coordinates.reverse();
						roads.push({node_from:rows[i].node_to, node_to:rows[i].node_from,cost:rows[i].cost,length:rows[i].length,geometry:JSON.stringify(geom)});
					}
					m = roads.length;
					//сортируем
					roads.sort(function(x,y){ return x.node_from-y.node_from});
					
					var curr_from = 0;
					var prev_from = 0;
					for ( var i = 0; i < m; prev_from = curr_from,i++ ){
						curr_from = roads[i].node_from;
						if ( curr_from != prev_from ){ //если from новый записываем его начальный индекс в index_from
							if ( curr_from - 1 > index_from.length ){
								for ( var j = 0; j < (curr_from - 1 - index_from.length); j++ ){
									index_from.push(-1);
									index_size.push(0);
								}
							}
							index_from.push(i);
							index_size.push(1);
						}else{ //если from старый увеличиваем последний index_size
							index_size[index_size.length-1]++;
						}
					}			
				}
			}
			callback();
		});
	});
}
コード例 #8
0
 db.serialize(function() {

    writer.pipe(fs.createWriteStream('./data/out/geojson/historic_borders.geojson'))
    
    db.each("SELECT *, AsGeoJSON(Geometry) AS the_geom FROM historic_borders", function(err, row) {
      if (err) console.log(err)

      var f = {}
      f.type = "Feature"
      f.properties = {}
      f.properties.address = row.Märkus      
      f.geometry = JSON.parse(row.the_geom)
      writer.write(f)
      
    }, function() { 
      writer.end() 
    });
  
  
});
コード例 #9
0
        }, function(err, results) {
          
        db.each("SELECT * FROM osiliana_map WHERE col_0 = '" + f.properties.address + "' LIMIT 1", function(err, row) {
          if (err) console.log(err)
          if (row && row.PK_UID !== 2) {
          //  f.properties.osiliana_url = base_osiliana + '/' + path.basename(row.col_3)
            f.properties.osiliana_url = base_osiliana + '/' + path.basename(row.col_3)
            f.properties.osiliana_desc = row.desc
              .replace(/<a\b[^>]*>/ig,'<strong>')
              .replace(/<\/a>/ig, '</strong>')
              .replace(/\n\n<\/p>/g,'</p>\n')
              .replace(/<h1>/g,'<h3>')
              .replace(/<\/h1>/g,'</h3>')
          }
        }, function() {
          writer.write(f)
          count++
        })
   
        
 }) 
コード例 #10
0
    }, function() {
  
    f.properties.historic_photos = []
  
    db.each("SELECT * FROM historic_photos WHERE asukoht = '" + f.properties.address + "';", function(err, row) {
      if (err) console.log(err)
      if (row) {
          var file = row.foto.split('\\')[4]
          file = file ? file.replace(/õ/g, 'o').replace(/ä/g, 'a').replace(/ö/g, 'o').replace(/ü/g, 'u') : ''
          f.properties.historic_photos.push({
            url: base_historic_photo + '/' + file,
            desc: row.selgitus,
            year: row.aasta, // @todo cleanup
            angle: row.pildistami
          })
        }
        }, function(err, results) {
          
        db.each("SELECT * FROM osiliana_map WHERE col_0 = '" + f.properties.address + "' LIMIT 1", function(err, row) {
          if (err) console.log(err)
          if (row && row.PK_UID !== 2) {
          //  f.properties.osiliana_url = base_osiliana + '/' + path.basename(row.col_3)
            f.properties.osiliana_url = base_osiliana + '/' + path.basename(row.col_3)
            f.properties.osiliana_desc = row.desc
              .replace(/<a\b[^>]*>/ig,'<strong>')
              .replace(/<\/a>/ig, '</strong>')
              .replace(/\n\n<\/p>/g,'</p>\n')
              .replace(/<h1>/g,'<h3>')
              .replace(/<\/h1>/g,'</h3>')
          }
        }, function() {
          writer.write(f)
          count++
        })
   
        
 }) 
    
    
  });
コード例 #11
0
ファイル: spatialite.js プロジェクト: psm21-vagrant/test
	db.spatialite(function(err) {
		db.all(sql, function(err, rows) {
			if ( rows != undefined ){
				if ( rows != null ){
					//записываем в массив
					var geom = null;
					for ( var i = 0; i < rows.length; i++ ){
						roads.push(rows[i]);
						geom = JSON.parse(rows[i].geometry);
						geom.coordinates.reverse();
						roads.push({node_from:rows[i].node_to, node_to:rows[i].node_from,cost:rows[i].cost,length:rows[i].length,geometry:JSON.stringify(geom)});
					}
					m = roads.length;
					//сортируем
					roads.sort(function(x,y){ return x.node_from-y.node_from});
					
					var curr_from = 0;
					var prev_from = 0;
					for ( var i = 0; i < m; prev_from = curr_from,i++ ){
						curr_from = roads[i].node_from;
						if ( curr_from != prev_from ){ //если from новый записываем его начальный индекс в index_from
							if ( curr_from - 1 > index_from.length ){
								for ( var j = 0; j < (curr_from - 1 - index_from.length); j++ ){
									index_from.push(-1);
									index_size.push(0);
								}
							}
							index_from.push(i);
							index_size.push(1);
						}else{ //если from старый увеличиваем последний index_size
							index_size[index_size.length-1]++;
						}
					}			
				}
			}
			callback();
		});
	});
コード例 #12
0
db.spatialite(function(err) {
 db.serialize(function() {

    writer.pipe(fs.createWriteStream('./data/out/geojson/historic_photos.geojson'))
    
    db.each("SELECT asukoht, aasta, AsGeoJSON(Geometry) AS the_geom FROM historic_photos", function(err, row) {
      if (err) console.log(err)

      var f = {}
      f.type = "Feature"
      f.properties = {}
      f.properties.address = row.asukoht 
      f.properties.year = row.aasta      
      f.geometry = JSON.parse(row.the_geom)
      writer.write(f)
      
    }, function() { 
      writer.end() 
    });
  
  
});
});
コード例 #13
0
ファイル: spatialite.js プロジェクト: psm21-vagrant/test
function routeQuery(from, to, callback){
	var start = latlng2node_id(from);
    var end = latlng2node_id(to);
	console.log(start+':'+end);
	var sql = "SELECT AsGeoJSON(geometry) AS geometry FROM roads_net WHERE ";
	sql += "NodeFrom=" + start + " AND NodeTo=" + end; 
	sql += " LIMIT 1;"
    //console.log(sql);
	db.spatialite(function(err) {
		db.get(sql, function(err, row) {
            //console.log(JSON.stringify(row));
            route = [];
			if ( row != undefined ){
				if ( row.geometry != null ){
					var obj = JSON.parse(row.geometry);
					route = obj.coordinates;
				}
			}
			//console.log(JSON.stringify(reverse(route)));
            callback(reverse(route));
		});
	});
}
コード例 #14
0
https://github.com/osmcode/node-osmium/blob/master/demo/spatialite-output/index.js

var sqlite3 = require('spatialite');

var db = new sqlite3.Database('dbTest.db');
db.serialize();
db.spatialite();
db.run("PRAGMA synchronous = OFF;"); // otherwise it is very slow


db.run("SELECT InitSpatialMetaData('WGS84');");

db.run("CREATE TABLE points (osm_id INT, name TEXT);");

db.run("SELECT AddGeometryColumn('points', 'geom', 4326, 'POINT', 2);");


for (var i = 0; i < 100000 ; i++) 
{
	db.run("INSERT INTO points (osm_id, name, geom) VALUES ("+i+", 'second point',GeomFromText('POINT(2.02 3.03)', 4326));");
	console.log(i);

}
コード例 #15
0
    db.each("SELECT *, AsGeoJSON(Geometry) AS the_geom, AsGeoJSON(Centroid(Geometry)) AS latlon FROM buildings", function(err, row) {
      if (err) console.log(err)
     
      var f = {}
      f.type = "Feature"
      f.properties = {}
      
      // Ehitise otsing http://www.ehr.ee/v12.aspx?loc=0101&pageNr=3
      // Dokumendiotsing http://www.ehr.ee/v12.aspx?loc=0102
      // ehr_dok: 'http://www.ehr.ee/v12.aspx?loc=10000001&EhrKood_dokOtsing=106022402'

      f.properties.address = row.aadress 
      f.properties.year = row.ehit_aasta > 0 ? row.ehit_aasta : ''      
      f.properties.photo = encodeURI(row.foto_pikk)
      
      f.geometry = JSON.parse(row.the_geom)
      f.latlon = JSON.parse(row.latlon)
      
      db.each("SELECT selgitus, ehitusaast FROM historic_buildings2 WHERE aadress = '" + f.properties.address + "' LIMIT 1", function(err, row) {
          if (err) console.log(err)
          if (row) { 
            f.properties.desc = row.selgitus
            f.properties.year_historic = row.ehitusaast > 0 ? row.ehitusaast : ''
          }
        }, function() {
      
        f.properties.historic_photos = []
      
        db.each("SELECT * FROM historic_photos WHERE asukoht = '" + f.properties.address + "';", function(err, row) {
          if (err) console.log(err)
          if (row) {
              var file = row.foto.split('\\')[4]
              file = file ? file.replace(/õ/g, 'o').replace(/ä/g, 'a').replace(/ö/g, 'o').replace(/ü/g, 'u') : ''
              f.properties.historic_photos.push({
                url: base_historic_photo + '/' + file,
                desc: row.selgitus,
                year: row.aasta, // @todo cleanup
                angle: row.pildistami
              })
            }
            }, function(err, results) {
              
            db.each("SELECT * FROM osiliana_map WHERE col_0 = '" + f.properties.address + "' LIMIT 1", function(err, row) {
              if (err) console.log(err)
              if (row && row.PK_UID !== 2) {
              //  f.properties.osiliana_url = base_osiliana + '/' + path.basename(row.col_3)
                f.properties.osiliana_url = base_osiliana + '/' + path.basename(row.col_3)
                f.properties.osiliana_desc = row.desc
                  .replace(/<a\b[^>]*>/ig,'<strong>')
                  .replace(/<\/a>/ig, '</strong>')
                  .replace(/\n\n<\/p>/g,'</p>\n')
                  .replace(/<h1>/g,'<h3>')
                  .replace(/<\/h1>/g,'</h3>')
              }
            }, function() {
              writer.write(f)
              count++
            })
       
            
     }) 
        
        
      });
      
    }, function() { 
コード例 #16
0
ファイル: index.js プロジェクト: JBurant/node-osmium
 handler.on('done', function() {
     add_highway.finalize();
     add_amenity.finalize();
     db.close();
 });