@@ -673,7 +673,7 @@ def _box2d_transform(self, res):
673673 logger .error (msg , e )
674674 raise InternalServerError (msg ) from e
675675
676- def _parse_locations (self , transformer , res ):
676+ def _parse_locations (self , res ):
677677 logger .debug ("Parse location result: %s" , res )
678678 if not self .returnGeometry :
679679 attrs2Del = ['x' , 'y' , 'lon' , 'lat' , 'geom_st_box2d' ]
@@ -695,11 +695,13 @@ def _parse_locations(self, transformer, res):
695695 ) from error
696696 else :
697697 try :
698- pnt = (res ['x' ], res ['y' ])
699- x , y = transformer .transform (pnt [0 ], pnt [1 ])
698+ # Transform from WGS84 lon/lat (which are always available) to target SRID
699+ # instead of from the Swiss coordinates (which may be swapped)
700+ transformer_from_wgs84 = get_transformer (4326 , self .srid )
701+ x , y = transformer_from_wgs84 .transform (res ['lon' ], res ['lat' ])
700702 res ['x' ] = x
701703 res ['y' ] = y
702- except (pyproj .exceptions .CRSError ) as error :
704+ except (pyproj .exceptions .CRSError , KeyError ) as error :
703705 logger .error ("Error while converting point %s to %s" , res , self .srid )
704706 raise InternalServerError (
705707 f'Error while converting point({ res } ), to EPSG:{ self .srid } '
@@ -708,7 +710,6 @@ def _parse_locations(self, transformer, res):
708710
709711 def _parse_location_results (self , results , limit ):
710712 nb_address = 0
711- transformer = get_transformer (self .DEFAULT_SRID , self .srid )
712713 for result in self ._yield_matches (results ):
713714 origin = result ['attrs' ]['origin' ]
714715 layer_bod_id = self ._origin_to_layerbodid (origin )
@@ -725,14 +726,14 @@ def _parse_location_results(self, results, limit):
725726 self ._bbox_intersection (self .bbox , result ['attrs' ]['geom_st_box2d' ])
726727 )
727728 ):
728- result ['attrs' ] = self ._parse_locations (transformer , result ['attrs' ])
729+ result ['attrs' ] = self ._parse_locations (result ['attrs' ])
729730 self .results ['results' ].append (result )
730731 nb_address += 1
731732 else :
732733 if not self .bbox or self ._bbox_intersection (
733734 self .bbox , result ['attrs' ]['geom_st_box2d' ]
734735 ):
735- self ._parse_locations (transformer , result ['attrs' ])
736+ self ._parse_locations (result ['attrs' ])
736737 self .results ['results' ].append (result )
737738 if len (self .results ['results' ]) > 0 :
738739 self .results ['results' ] = self .results ['results' ][:limit ]
0 commit comments