2017-02-28 1 views
0

Ich arbeite mit Nodejs Postgres und Flugblatt, um eine Choroplethik-Karte zu erstellen. Ich habe eine Abfrage ausgeführt und funktioniert perfekt auf der Karte. Wie auch immer, ich kann nicht scheinen, dass ein anderes funktioniert, da ich möchte, dass dies Optionen sind, die ein Benutzer auswählen kann. Dies ist der Code, den ich habe. Ich bin übrigens neu dabei.Mehrere Anfragen an Postgres-Datenbank für Choroplethe Prospektkarte

// Set up database query to display GeoJSON 
    var OSM = "SELECT row_to_json(fc) FROM (SELECT 'FeatureCollection' As  type, array_to_json(array_agg(f)) As features FROM (SELECT 'Feature' As type,  ST_AsGeoJSON(lg.geom)::json As geometry, row_to_json((thirty_cens, name_tag)) As properties FROM civil_parishes As lg) As f) As fc"; 
//var OSM2 ="SELECT row_to_json(fc) FROM (SELECT 'FeatureCollection' As type, array_to_json(array_agg(f)) As features FROM (SELECT 'Feature' As type, ST_AsGeoJSON(lg.geom)::json As geometry, row_to_json((twenty_cens, name_tag)) As properties FROM civil_parishes As lg) As f) As fc"; 
/* GET home page. */ 
router.get('/', function(req, res) { 
    res.render('index', { 
     title: 'Web Mapping' 
    }); 
}); 

module.exports = router; 

/* GET Postgres JSON data */ 
router.get('/data', function (req, res) { 
    var client = new pg.Client(conString); 
    client.connect(); 
    var query = client.query(OSM); 
     query.on("row", function (row, result) { 
     result.addRow(row); 
    }); 
     query.on("end", function (result) { 
     res.send(result.rows[0].row_to_json); 
     res.end(); 
    }); 
    }); 

/* GET the map page */ 
router.get('/map', function(req, res) { 
    var client = new pg.Client(conString); // Setup Postgres Client 
    client.connect();      // connect to the client 
    var query = client.query(OSM);   // Run Query 
    query.on("row", function (row, result) { 
     result.addRow(row); 
     }); 
    // Pass the result to the map page 
    query.on("end", function (result) { 
     var data = result.rows[0].row_to_json // Save the JSON as variable data 
     res.render('map', { 
      title: "Web Mapping",    // Give a title to page 
      jsonData: data     // Pass data to the View 
     }); 
    }) 
}); 

Dies ist meine map.jade Seite:

#map(style='height: 100%; width: 100%') 
    script(src='http://cdn.leafletjs.com/leaflet/v0.7.7/leaflet.js') 
script(src='http://code.jquery.com/jquery-2.1.0.min.js') 
script. 
    var myData = !{JSON.stringify(jsonData)};// Create variable to hold map element, give initial settings to map 
    //var myData2 = !{JSON.stringify(jsonData2)};// Create variable to hold map element, give initial settings to map 
    var map = L.map('map').setView([53.2734, -7.778320310000026], 7); 
    var osmmap = L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'}).addTo(map); 
    // Add JSON to map 
    var censLayer=new L.geoJson(myData,{ 
     style:getStyle, 
     onEachFeature: onEachFeature 
     }); 
    //var censLayer2=new L.geoJson(myData2,{ 
     //style:getStyle, 
     //onEachFeature: onEachFeature 
     /}); 
    function getStyle(feature) { 
     return { 
      weight: 1, 
      opacity: 1, 
      color: '#fff', 
      fillOpacity: 0.7, 
      fillColor: getColor(feature.properties.f1) 
     }; 
    } 
    function getColor(d) { 
     return d > 100 ? 'blue' : 
       d > 70 ? 'red' : 
       d > 50 ? 'green' : 
         'grey'; 
       } 
    function onEachFeature(feature, layer){ 
     layer.bindPopup(feature.properties.f2); 
    } 
    var baseMaps ={ 
     "osmmap": osmmap}; 
    var overlayMaps = { 
     "censLayer": censLayer}; 
    L.control.layers(baseMaps,overlayMaps).addTo(map); 

Ich weiß, dass die OSM2 Abfrage ausgeführt wird und Verknüpfung mit cwnsLayer2 funktioniert nicht. Kann jemand bitte einen Rat geben, warum es nicht funktioniert?

Antwort

0

Disclaimer Ich bin auch sehr neu, und das passiert zufällig. Vielleicht nicht die beste Lösung.

Ich hatte das gleiche Problem, wo ich konnte nicht mehrere Abfragen zu arbeiten. Nach der Frage um, das ist die Lösung, die ich kam mit:

router.get('/map', function(req, res) { 
    var client = new pg.Client(conString); 
    client.connect(); 

    var dataHolder = []; //use this to store you results to JSON 

    var query = client.query(OSM);   
    query.on("row", function (row, result) { 
      result.addRow(row); 
    }); 

    query.on("end", function (result) { 
     var data = result.rows[0].row_to_json; 

     dataHolder.push(data) // add this 


     // this will run your second query 
     var query = client.query(OSM2);   
     query.on("row", function (row, result) { 
      result.addRow(row); 
     }); 

     query.on("end", function (result) { 
      var data2 = result.rows[0].row_to_json; 

      dataHolder.push(data2) // add this 

      res.render('map', { 
       title: "Web Mapping",    
       jsonData: data[0] 
       jsonData2: data[1] 
      });      
     }); 
    }) 
}); 

Wenn Sie über eine bessere Lösung kommen, bitte teilen: D

Verwandte Themen