YinaZ / AccessMapLite

0 stars 0 forks source link

Save query content elsewhere (other than using a super long string #1

Closed YinaZ closed 7 years ago

YinaZ commented 7 years ago

In app.js line 30, I use sequelize.query() function to raw query the database, get the nearest start & end point, do the routing based on elevation and return the geoJSON of the edges of the shortest path. This query is super long, and currently I just use a var "query" to hold the whole string. Code is shown below:

  var query = (
    'CREATE OR REPLACE FUNCTION getRoute(source integer, target integer) '
  + '  RETURNS TABLE(seq integer, path_seq integer, node bigint, edge bigint, cost double precision, agg_cost double precision) AS $$ '
  + '  SELECT d.seq, d.path_seq, d.node, d.edge, d.cost, d.agg_cost '
  + '    FROM pgr_dijkstra(\'SELECT osm_id AS id, source, target, '
  + 'grade + ST_Length(geom) AS cost FROM routing_info WHERE grade IS NOT NULL\', $1, $2, false) AS d; '
  + '$$ LANGUAGE sql; '
  + 'CREATE OR REPLACE FUNCTION getNearestNode (geom text) '
  + 'RETURNS integer AS $$ '
  + '   SELECT CAST (id AS integer) FROM routing_info_vertices_pgr ORDER BY the_geom <-> ST_Transform(ST_GeometryFromText(ST_AsText(ST_GeomFromGeoJSON(geom)),4326), 900913) LIMIT 1; '
  + '$$ LANGUAGE sql; '
  + 'SELECT ST_AsGeoJSON(ST_Transform(geom, 4326)) AS geojson FROM routing_info WHERE osm_id IN (SELECT edge FROM getRoute(getNearestNode(?), getNearestNode(?)))');
  sequelize.query(query,
    { replacements: [req.body.source, req.body.target], type: sequelize.QueryTypes.SELECT }).then(......){}

Since "query" is only a long string, there should be ways to save the content of "query" (such as into a static file, actually even a plain text file works) and read the query content every time, so the code in app.js will look cleaner.

YinaZ commented 7 years ago

I changed the string to a multi-line string and it looks much better.