fix geodesic error on azimuth
This commit is contained in:
		
							parent
							
								
									efea6fe13c
								
							
						
					
					
						commit
						1f1502a623
					
				| 
						 | 
				
			
			@ -20,7 +20,7 @@ export abstract class Algorithm {
 | 
			
		|||
    for (const processor of this.processors) {
 | 
			
		||||
      this.candidates = await processor.execute(this.candidates);
 | 
			
		||||
    }
 | 
			
		||||
    // console.log(JSON.stringify(this.candidates, null, 2));
 | 
			
		||||
    console.log(JSON.stringify(this.candidates, null, 2));
 | 
			
		||||
    return this.candidates.map((candidate: CandidateEntity) =>
 | 
			
		||||
      MatchEntity.create({ adId: candidate.id }),
 | 
			
		||||
    );
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -20,6 +20,7 @@ export class PassengerOrientedAlgorithm extends Algorithm {
 | 
			
		|||
      new PassengerOrientedCarpoolPathCompleter(query),
 | 
			
		||||
      new RouteCompleter(query, RouteCompleterType.BASIC),
 | 
			
		||||
      new PassengerOrientedGeoFilter(query),
 | 
			
		||||
      new RouteCompleter(query, RouteCompleterType.DETAILED),
 | 
			
		||||
    ];
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -8,4 +8,6 @@ export interface GeodesicPort {
 | 
			
		|||
    azimuth: number;
 | 
			
		||||
    distance: number;
 | 
			
		||||
  };
 | 
			
		||||
  distance(lon1: number, lat1: number, lon2: number, lat2: number): number;
 | 
			
		||||
  azimuth(lon1: number, lat1: number, lon2: number, lat2: number): number;
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -14,6 +14,7 @@ import { Geodesic } from './infrastructure/geodesic';
 | 
			
		|||
import { GraphhopperGeorouter } from './infrastructure/graphhopper-georouter';
 | 
			
		||||
import { HttpModule } from '@nestjs/axios';
 | 
			
		||||
import { GetRouteQueryHandler } from './core/application/queries/get-route/get-route.query-handler';
 | 
			
		||||
import { GetDetailedRouteController } from './interface/controllers/get-detailed-route.controller';
 | 
			
		||||
 | 
			
		||||
const queryHandlers: Provider[] = [GetRouteQueryHandler];
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -37,11 +38,17 @@ const adapters: Provider[] = [
 | 
			
		|||
    useClass: Geodesic,
 | 
			
		||||
  },
 | 
			
		||||
  GetBasicRouteController,
 | 
			
		||||
  GetDetailedRouteController,
 | 
			
		||||
];
 | 
			
		||||
 | 
			
		||||
@Module({
 | 
			
		||||
  imports: [CqrsModule, HttpModule],
 | 
			
		||||
  providers: [...queryHandlers, ...mappers, ...adapters],
 | 
			
		||||
  exports: [RouteMapper, DIRECTION_ENCODER, GetBasicRouteController],
 | 
			
		||||
  exports: [
 | 
			
		||||
    RouteMapper,
 | 
			
		||||
    DIRECTION_ENCODER,
 | 
			
		||||
    GetBasicRouteController,
 | 
			
		||||
    GetDetailedRouteController,
 | 
			
		||||
  ],
 | 
			
		||||
})
 | 
			
		||||
export class GeographyModule {}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -22,7 +22,38 @@ export class Geodesic implements GeodesicPort {
 | 
			
		|||
      lat2,
 | 
			
		||||
      lon2,
 | 
			
		||||
    );
 | 
			
		||||
    if (!azimuth || !distance) throw new Error('Azimuth not found');
 | 
			
		||||
    if (!azimuth || !distance)
 | 
			
		||||
      throw new Error(
 | 
			
		||||
        `Inverse not found for coordinates ${lon1} ${lat1} / ${lon2} ${lat2}`,
 | 
			
		||||
      );
 | 
			
		||||
    return { azimuth, distance };
 | 
			
		||||
  };
 | 
			
		||||
 | 
			
		||||
  azimuth = (
 | 
			
		||||
    lon1: number,
 | 
			
		||||
    lat1: number,
 | 
			
		||||
    lon2: number,
 | 
			
		||||
    lat2: number,
 | 
			
		||||
  ): number => {
 | 
			
		||||
    const { azi2: azimuth } = this.geod.Inverse(lat1, lon1, lat2, lon2);
 | 
			
		||||
    if (!azimuth)
 | 
			
		||||
      throw new Error(
 | 
			
		||||
        `Azimuth not found for coordinates ${lon1} ${lat1} / ${lon2} ${lat2}`,
 | 
			
		||||
      );
 | 
			
		||||
    return azimuth;
 | 
			
		||||
  };
 | 
			
		||||
 | 
			
		||||
  distance = (
 | 
			
		||||
    lon1: number,
 | 
			
		||||
    lat1: number,
 | 
			
		||||
    lon2: number,
 | 
			
		||||
    lat2: number,
 | 
			
		||||
  ): number => {
 | 
			
		||||
    const { s12: distance } = this.geod.Inverse(lat1, lon1, lat2, lon2);
 | 
			
		||||
    if (!distance)
 | 
			
		||||
      throw new Error(
 | 
			
		||||
        `Distance not found for coordinates ${lon1} ${lat1} / ${lon2} ${lat2}`,
 | 
			
		||||
      );
 | 
			
		||||
    return distance;
 | 
			
		||||
  };
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -192,14 +192,14 @@ export class GraphhopperGeorouter implements GeorouterPort {
 | 
			
		|||
      .filter((element) => element.index == -1);
 | 
			
		||||
    for (const index in points) {
 | 
			
		||||
      for (const missedWaypoint of missedWaypoints) {
 | 
			
		||||
        const inverse = this.geodesic.inverse(
 | 
			
		||||
        const distance = this.geodesic.distance(
 | 
			
		||||
          missedWaypoint.waypoint[0],
 | 
			
		||||
          missedWaypoint.waypoint[1],
 | 
			
		||||
          points[index][0],
 | 
			
		||||
          points[index][1],
 | 
			
		||||
        );
 | 
			
		||||
        if (inverse.distance < missedWaypoint.distance) {
 | 
			
		||||
          missedWaypoint.distance = inverse.distance;
 | 
			
		||||
        if (distance < missedWaypoint.distance) {
 | 
			
		||||
          missedWaypoint.distance = distance;
 | 
			
		||||
          missedWaypoint.nearest = parseInt(index);
 | 
			
		||||
        }
 | 
			
		||||
      }
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -11,4 +11,26 @@ describe('Matcher geodesic', () => {
 | 
			
		|||
    expect(Math.round(inv.azimuth as number)).toBe(45);
 | 
			
		||||
    expect(Math.round(inv.distance as number)).toBe(156900);
 | 
			
		||||
  });
 | 
			
		||||
  it('should get azimuth value', () => {
 | 
			
		||||
    const geodesic: Geodesic = new Geodesic();
 | 
			
		||||
    const azimuth = geodesic.azimuth(0, 0, 1, 1);
 | 
			
		||||
    expect(Math.round(azimuth as number)).toBe(45);
 | 
			
		||||
  });
 | 
			
		||||
  it('should get distance value', () => {
 | 
			
		||||
    const geodesic: Geodesic = new Geodesic();
 | 
			
		||||
    const distance = geodesic.distance(0, 0, 1, 1);
 | 
			
		||||
    expect(Math.round(distance as number)).toBe(156900);
 | 
			
		||||
  });
 | 
			
		||||
  it('should throw an exception if inverse fails', () => {
 | 
			
		||||
    const geodesic: Geodesic = new Geodesic();
 | 
			
		||||
    expect(() => {
 | 
			
		||||
      geodesic.inverse(7.74547, 48.583035, 7.74547, 48.583036);
 | 
			
		||||
    }).toThrow();
 | 
			
		||||
  });
 | 
			
		||||
  it('should throw an exception if azimuth fails', () => {
 | 
			
		||||
    const geodesic: Geodesic = new Geodesic();
 | 
			
		||||
    expect(() => {
 | 
			
		||||
      geodesic.azimuth(7.74547, 48.583035, 7.74547, 48.583036);
 | 
			
		||||
    }).toThrow();
 | 
			
		||||
  });
 | 
			
		||||
});
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -253,6 +253,8 @@ const mockGeodesic: GeodesicPort = {
 | 
			
		|||
    azimuth: 45,
 | 
			
		||||
    distance: 50000,
 | 
			
		||||
  })),
 | 
			
		||||
  azimuth: jest.fn().mockImplementation(() => 45),
 | 
			
		||||
  distance: jest.fn().mockImplementation(() => 50000),
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
const mockDefaultParamsProvider: DefaultParamsProviderPort = {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue