Skip to content

Commit

Permalink
Merge pull request #104 from Arquisoft/fix-geocoder
Browse files Browse the repository at this point in the history
Fix geocoder
  • Loading branch information
jbritosm authored Apr 3, 2022
2 parents bc770fd + 39dd545 commit eed40e5
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 29 deletions.
42 changes: 26 additions & 16 deletions restapi/geocoder/geocoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,22 @@ const Nominatim = require('nominatim-geocoder')
const geocoder = new Nominatim()

type Location = {
street: string,
city: string,
street?: string,
city?: string,
region?: string
country?: string,
}

function tryGeocode(query: string, errorMsg: string = "") {
async function tryGeocode(query: string, errorMsg: string = "") {
return geocoder.search({q: query})
.then((response : any) => {
if (response[0] != {}) {
let lat : number = response[0]["lat"]
let lon : number = response[0]["lon"]
let distance : number = distanceInKmBetweenEarthCoordinates(lat, lon, 43.354838799999996, -5.851292403149609)
console.log(response)
return {
"distance" : distance,
"price" : distance * parseInt(process.env.PRICE_PER_KM || ""),
"price" : distance * parseFloat(process.env.PRICE_PER_KM || ""),
"error" : errorMsg,
"success" : true
}
Expand All @@ -38,27 +37,38 @@ function tryGeocode(query: string, errorMsg: string = "") {
})
}

export function getPriceFromAddress(address: Location) {
let geocodeTry = tryGeocode(address.city + ", " + address.street)
if (geocodeTry.success) return geocodeTry
export async function getPriceFromAddress(address: Location) {

if (address.region == null)
if (address.street == null || address.street == '') {
return {"error" : "needed street information"}
}
if (address.city == null || address.city == '') {
return {"error" : "needed city information"}
}
if (address.region == null || address.region == '') {
return {"error" : "not enough info provided, region is needed"}
}
if (address.country == null || address.country == '') {
return {"error" : "not enough info provided, city is needed"}
}


let geocodeTry = await tryGeocode(address.city + ", " + address.street)
if (geocodeTry.success) return geocodeTry

geocodeTry = tryGeocode(address.region + ", " + address.street)
geocodeTry = await tryGeocode(address.region + ", " + address.street)
if (geocodeTry.success) return geocodeTry

geocodeTry = tryGeocode(address.city, "failed specific info, falling back to city")
geocodeTry = await tryGeocode(address.region + ", " + address.city, "failed specific info, falling back to city")
if (geocodeTry.success) return geocodeTry

geocodeTry = tryGeocode(address.region, "failed specific info, falling back to region")
geocodeTry = await tryGeocode("Province: " + address.region, "failed specific info, falling back to region")
if (geocodeTry.success) return geocodeTry

if (address.country == null)
return {"error" : "not enough info provided, city is needed"}

geocodeTry = tryGeocode(address.country, "failed specific info, falling back to city")
geocodeTry = await tryGeocode("Country: " + address.country, "failed specific info, falling back to country")
if (geocodeTry.success) return geocodeTry

return {"error" : "Address not found"}
}


Expand Down
1 change: 1 addition & 0 deletions restapi/interfaces/OrderInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import IProduct from "./ProductInterface";
export default interface IOrder extends Document {
webId: string
addres: string
name: string
shippingPrice: number
totalPrice: number
products: Map<IProduct, Number>
Expand Down
23 changes: 23 additions & 0 deletions restapi/routers/geocoderRouter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

import { Router, Request, Response} from 'express';
import * as bodyParser from 'body-parser';
import { getPriceFromAddress } from '../geocoder/geocoder';

const geocoderRouter:Router = Router()

geocoderRouter.use(bodyParser.json());

geocoderRouter.post("", async (req: Request, res: Response) => {
await getPriceFromAddress(req.body)
.then((response : any) => {
res.status(200).send(response);
console.log(response)
}).catch((error : any) => {
res.status(500).json({
message: error.message,
error
})
})
})

export default geocoderRouter;
1 change: 1 addition & 0 deletions restapi/schemas/OrderSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const orderSchema = new Schema (
{
webId: {type: String, required: true},
address: {type: String, required: true},
name: {type: String, required: false },
shippingPrice: {type: Number, required: true},
totalPrice: {type: Number, required: true},
products: {type: Map, of: Number, required: true}
Expand Down
15 changes: 2 additions & 13 deletions restapi/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import sellerRouter from "./routers/SellerRouter";
import productRouter from "./routers/ProductRouter";
import orderRouter from "./routers/OrderRouter";
import solidRouter from "./solid/solidRouter";
import geocoderRouter from "./routers/geocoderRouter";

import { getPriceFromAddress } from "./geocoder/geocoder";

import 'dotenv/config'

Expand All @@ -32,19 +32,8 @@ app.use("/seller", sellerRouter)
app.use("/product", productRouter)
app.use("/order", orderRouter)
app.use("/solid", solidRouter)
app.use("/geocode", geocoderRouter)

app.get("/geocode/:address", async (req: Request, res: Response) => {
getPriceFromAddress(req.body)
.then((response : any) => {
res.status(200).send(response);
console.log(response)
}).catch((error : any) => {
res.status(500).json({
message: error.message,
error
})
})
})

// Connect to the database and start the server.
mongoose.connect('mongodb+srv://cluster0.2sj0r.mongodb.net/', {
Expand Down

0 comments on commit eed40e5

Please sign in to comment.