         res.status(500).end()
       else
         res.end()
+
+
+validateParameters = (req,res,next) ->
+  if(distanceKm(req.query.nelat, req.query.nelng, req.query.swlat, req.query.swlng) > 10.0)
+    res.json([])
+    return
+  if req.query.nelat is undefined or req.query.swlat is undefined or req.query.nelng is undefined or req.query.swlng is undefined
+    res.json([])
+    return
+  next()
     
 # get measurements for wifi access points with GET /measurement/wifi
-router.get '/wifi', (req, res, next) ->
-  points = []
-  if(distanceKm(req.query.nelat, req.query.nelng, req.query.swlat, req.query.swlng) > 10.0)
-    res.json(points)
-    return
-  if req.query.nelat is undefined or req.query.swlat is undefined or req.query.nelng is undefined or req.query.swlng is undefined
-    res.json(points)
-    return
+router.get '/wifi', validateParameters, (req, res, next) ->
   Measurement.find(
     location:
       $geoWithin:

    if err
      res.status(500)
      return
    for point in data
      points.push([point.location.coordinates[0], point.location.coordinates[1],  parseFloat(point.signalDBm) / 31.0])
    res.json(points)


distanceKm = (lat1, lon1, lat2, lon2) -> 

    if err
      res.status(500)
      return
    res.json(data.map (item) ->
      return [item.location.coordinates[0], item.location.coordinates[1], 0.5]
    )
  )

# get measurements for signal strength with GET /measurement/signal
router.get '/signal', validateParameters, (req, res, next) ->
  Measurement.find
    location:
      $geoWithin:
