@@ -491,12 +491,26 @@ app.post("/api/estate/nazotte", async (req, res, next) => {
491491 } ,
492492 } ;
493493
494+ const coordinatesToText = util . format (
495+ "'POLYGON((%s))'" ,
496+ coordinates
497+ . map ( ( coordinate ) =>
498+ util . format ( "%f %f" , coordinate . latitude , coordinate . longitude )
499+ )
500+ . join ( "," )
501+ ) ;
502+
503+ const sql = util . format (
504+ "SELECT * FROM estate WHERE latitude <= ? AND latitude >= ? AND longitude <= ? AND longitude >= ? AND ST_Contains(ST_PolygonFromText(%s), ST_GeomFromText(CONCAT('POINT(', latitude, ' ', longitude, ')'))) ORDER BY popularity DESC, id ASC" ,
505+ coordinatesToText
506+ )
507+
494508 const getConnection = promisify ( db . getConnection . bind ( db ) ) ;
495509 const connection = await getConnection ( ) ;
496510 const query = promisify ( connection . query . bind ( connection ) ) ;
497511 try {
498512 const estates = await query (
499- "SELECT * FROM estate WHERE latitude <= ? AND latitude >= ? AND longitude <= ? AND longitude >= ? ORDER BY popularity DESC, id ASC" ,
513+ sql ,
500514 [
501515 boundingbox . bottomright . latitude ,
502516 boundingbox . topleft . latitude ,
@@ -505,35 +519,11 @@ app.post("/api/estate/nazotte", async (req, res, next) => {
505519 ]
506520 ) ;
507521
508- const estatesInPolygon = [ ] ;
509- for ( const estate of estates ) {
510- const point = util . format (
511- "'POINT(%f %f)'" ,
512- estate . latitude ,
513- estate . longitude
514- ) ;
515- const sql =
516- "SELECT * FROM estate WHERE id = ? AND ST_Contains(ST_PolygonFromText(%s), ST_GeomFromText(%s))" ;
517- const coordinatesToText = util . format (
518- "'POLYGON((%s))'" ,
519- coordinates
520- . map ( ( coordinate ) =>
521- util . format ( "%f %f" , coordinate . latitude , coordinate . longitude )
522- )
523- . join ( "," )
524- ) ;
525- const sqlstr = util . format ( sql , coordinatesToText , point ) ;
526- const [ e ] = await query ( sqlstr , [ estate . id ] ) ;
527- if ( e && Object . keys ( e ) . length > 0 ) {
528- estatesInPolygon . push ( e ) ;
529- }
530- }
531-
532522 const results = {
533523 estates : [ ] ,
534524 } ;
535525 let i = 0 ;
536- for ( const estate of estatesInPolygon ) {
526+ for ( const estate of estates ) {
537527 if ( i >= NAZOTTE_LIMIT ) {
538528 break ;
539529 }
0 commit comments