Commit 68bc9eaf authored by Simon Pastor's avatar Simon Pastor
Browse files

final merge gis-engine

parent b48784e3
Loading
Loading
Loading
Loading
+20 −6
Original line number Diff line number Diff line
@@ -250,7 +250,7 @@ paths:
        500:
          description: "Internal server error"
  /geodata/{assetName}/distanceTo:
    get:
    post:
      tags:
      - "Geospatial Data"
      summary: "Get distance between geospatial data points"
@@ -283,7 +283,7 @@ paths:
        500:
          description: "Internal server error"
  /geodata/{assetName}/withinRange:
    get:
    post:
      tags:
      - "Geospatial Data"
      summary: "Returns if a geospatial data points is within a specified distance\
@@ -374,14 +374,26 @@ definitions:
      longitude: 6.0274563
  DistanceResponse:
    type: "object"
    required:
    - "distance"
    properties:
      distance:
        type: "number"
        format: "float"
        description: "Distance between two points (in meters)"
      latitude:
        type: "number"
        format: "float"
        description: "Destination asset latitude"
      longitude:
        type: "number"
        format: "float"
        description: "Destination asset longitude"
    description: "Distance response"
    example:
      distance: 0.8008282
      latitude: 6.0274563
      longitude: 1.4658129
  GeoDataAssetList:
    type: "object"
    properties:
@@ -458,15 +470,17 @@ definitions:
      longitude: 6.0274563
  WithinRangeResponse:
    type: "object"
    required:
    - "within"
    properties:
      latitude:
        type: "number"
        format: "float"
        description: "Asset latitude"
      lontitude:
        description: "Destination asset latitude"
      longitude:
        type: "number"
        format: "float"
        description: "Asset longitude"
        description: "Destination asset longitude"
      within:
        type: "boolean"
        description: "Within range result (e.g. true = within range, false = beyond\
@@ -475,7 +489,7 @@ definitions:
    example:
      within: true
      latitude: 0.8008282
      lontitude: 6.0274563
      longitude: 6.0274563
  GeoData:
    type: "object"
    properties:
+1 −1
Original line number Diff line number Diff line
@@ -13,7 +13,7 @@ To see how to make this your own, look here:
[README](https://github.com/swagger-api/swagger-codegen/blob/master/README.md)

- API version: 1.0.0
- Build date: 2021-05-28T11:51:37.253-04:00
- Build date: 2021-05-31T09:07:11.391-04:00


### Running the server
+186 −53
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@ import (
	"fmt"
	"net/http"
	"os"
	"strconv"
	"strings"
	"sync"
	"time"
@@ -1106,11 +1107,22 @@ func geGetAssetData(w http.ResponseWriter, r *http.Request) {
	fmt.Fprint(w, string(jsonResponse))
}

func convertJsonToPoint(jsonData string) *Point {

	var obj Point
	err := json.Unmarshal([]byte(jsonData), &obj)
	if err != nil {
		log.Error(err.Error())
		return nil
	}
	return &obj
}

func geGetDistanceGeoDataByName(w http.ResponseWriter, r *http.Request) {
	// Get asset name from request path parameters
	vars := mux.Vars(r)
	assetName := vars["assetName"]
	log.Debug("Get GeoData for asset: ", assetName)
	log.Debug("Get Distance GeoData for asset: ", assetName)

	// Make sure scenario is active
	if ge.activeModel.GetScenarioName() == "" {
@@ -1120,12 +1132,25 @@ func geGetDistanceGeoDataByName(w http.ResponseWriter, r *http.Request) {
		return
	}

	// Find asset in active scenario model
	node := ge.activeModel.GetNode(assetName)
	if node == nil {
		err := errors.New("Asset not found in active scenario")
	srcAsset := ge.assets[assetName]
	if srcAsset == nil {
		err := errors.New("Asset not in scenario")
		log.Error(err.Error())
		http.Error(w, err.Error(), http.StatusNotFound)
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}

	srcLongStr := ""
	srcLatStr := ""
	if srcAsset.geoData != nil {
		srcPosition := srcAsset.geoData.position
		srcPoint := convertJsonToPoint(srcPosition)
		srcLongStr = strconv.FormatFloat(float64(srcPoint.Coordinates[0]), 'f', -1, 32)
		srcLatStr = strconv.FormatFloat(float64(srcPoint.Coordinates[1]), 'f', -1, 32)
	} else {
		err := errors.New("Asset has no geo location")
		log.Error(err.Error())
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}

@@ -1145,20 +1170,55 @@ func geGetDistanceGeoDataByName(w http.ResponseWriter, r *http.Request) {
		return
	}

	dstLong := float32(0.0)
	dstLat := float32(0.0)
	dstLongStr := ""
	dstLatStr := ""
	if distanceParam.AssetName != "" {

		// Find second asset in active scenario model
		node := ge.activeModel.GetNode(assetName)
		if node == nil {
			err := errors.New("Asset not found in active scenario")
		dstAsset := ge.assets[distanceParam.AssetName]
		if dstAsset == nil {
			err := errors.New("Destination asset not in scenario")
			log.Error(err.Error())
			http.Error(w, err.Error(), http.StatusNotFound)
			http.Error(w, err.Error(), http.StatusBadRequest)
			return
		}
		if dstAsset.geoData != nil {
			dstPosition := dstAsset.geoData.position
			dstPoint := convertJsonToPoint(dstPosition)
			dstLong = dstPoint.Coordinates[0]
			dstLat = dstPoint.Coordinates[1]
			dstLongStr = strconv.FormatFloat(float64(dstPoint.Coordinates[0]), 'f', -1, 32)
			dstLatStr = strconv.FormatFloat(float64(dstPoint.Coordinates[1]), 'f', -1, 32)
		} else {
			err := errors.New("Destination asset has no geo location")
			log.Error(err.Error())
			http.Error(w, err.Error(), http.StatusBadRequest)
			return
		}
	} else {
		dstLong = distanceParam.Longitude
		dstLat = distanceParam.Latitude
		dstLongStr = strconv.FormatFloat(float64(distanceParam.Longitude), 'f', -1, 32)
		dstLatStr = strconv.FormatFloat(float64(distanceParam.Latitude), 'f', -1, 32)
	}

	srcCoordinates := "(" + srcLongStr + " " + srcLatStr + ")"
	dstCoordinates := "(" + dstLongStr + " " + dstLatStr + ")"

	distance, err := ge.assetMgr.GetDistanceBetweenPoints(srcCoordinates, dstCoordinates)
	if err != nil {
		log.Error(err.Error())
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	// Create Response to return
	var resp DistanceResponse
	resp.Distance = distance
	resp.Longitude = dstLong
	resp.Latitude = dstLat

	// Format response
	jsonResponse, err := json.Marshal(&resp)
@@ -1175,15 +1235,11 @@ func geGetDistanceGeoDataByName(w http.ResponseWriter, r *http.Request) {
}

func geGetWithinRangeGeoDataByName(w http.ResponseWriter, r *http.Request) {
	/*

	// Get asset name from request path parameters
	vars := mux.Vars(r)
	assetName := vars["assetName"]
	   log.Debug("Get GeoData for asset: ", assetName)

	   // Retrieve query parameters
	   query := r.URL.Query()
	   excludePath := query.Get("excludePath")
	log.Debug("Get Within Range GeoData for asset: ", assetName)

	// Make sure scenario is active
	if ge.activeModel.GetScenarioName() == "" {
@@ -1193,20 +1249,98 @@ func geGetWithinRangeGeoDataByName(w http.ResponseWriter, r *http.Request) {
		return
	}

	   // Find asset in active scenario model
	   node := ge.activeModel.GetNode(assetName)
	   if node == nil {
	           err := errors.New("Asset not found in active scenario")
	srcAsset := ge.assets[assetName]
	if srcAsset == nil {
		err := errors.New("Asset not in scenario")
		log.Error(err.Error())
	           http.Error(w, err.Error(), http.StatusNotFound)
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}

	   // Create GeoData Asset to return
	   var asset GeoDataAsset
	srcLongStr := ""
	srcLatStr := ""
	if srcAsset.geoData != nil {
		srcPosition := srcAsset.geoData.position
		srcPoint := convertJsonToPoint(srcPosition)
		srcLongStr = strconv.FormatFloat(float64(srcPoint.Coordinates[0]), 'f', -1, 32)
		srcLatStr = strconv.FormatFloat(float64(srcPoint.Coordinates[1]), 'f', -1, 32)
	} else {
		err := errors.New("Asset has no geo location")
		log.Error(err.Error())
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}

	// Retrieve Within Range parameters from request body
	var withinRangeParam WithinRangeParameters
	if r.Body == nil {
		err := errors.New("Request body is missing")
		log.Error(err.Error())
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}
	decoder := json.NewDecoder(r.Body)
	err := decoder.Decode(&withinRangeParam)
	if err != nil {
		log.Error(err.Error())
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	dstLong := float32(0.0)
	dstLat := float32(0.0)
	dstLongStr := ""
	dstLatStr := ""
	if withinRangeParam.AssetName != "" {

		// Find second asset in active scenario model
		dstAsset := ge.assets[withinRangeParam.AssetName]
		if dstAsset == nil {
			err := errors.New("Destination asset not in scenario")
			log.Error(err.Error())
			http.Error(w, err.Error(), http.StatusBadRequest)
			return
		}
		if dstAsset.geoData != nil {
			dstPosition := dstAsset.geoData.position
			dstPoint := convertJsonToPoint(dstPosition)
			dstLong = dstPoint.Coordinates[0]
			dstLat = dstPoint.Coordinates[1]
			dstLongStr = strconv.FormatFloat(float64(dstPoint.Coordinates[0]), 'f', -1, 32)
			dstLatStr = strconv.FormatFloat(float64(dstPoint.Coordinates[1]), 'f', -1, 32)
		} else {
			err := errors.New("Destination asset has no geo location")
			log.Error(err.Error())
			http.Error(w, err.Error(), http.StatusBadRequest)
			return
		}

	} else {
		dstLong = withinRangeParam.Longitude
		dstLat = withinRangeParam.Latitude
		dstLongStr = strconv.FormatFloat(float64(withinRangeParam.Longitude), 'f', -1, 32)
		dstLatStr = strconv.FormatFloat(float64(withinRangeParam.Latitude), 'f', -1, 32)
	}

	srcCoordinates := "(" + srcLongStr + " " + srcLatStr + ")"
	dstCoordinates := "(" + dstLongStr + " " + dstLatStr + ")"
	radius := strconv.FormatFloat(float64(withinRangeParam.Radius), 'f', -1, 32)

	withinRange, err := ge.assetMgr.GetWithinRangeBetweenPoints(srcCoordinates, dstCoordinates, radius)
	if err != nil {
		log.Error(err.Error())
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	// Create Response to return
	var resp WithinRangeResponse
	resp.Within = withinRange
	resp.Longitude = dstLong
	resp.Latitude = dstLat

	// Format response
	   jsonResponse, err := json.Marshal(&asset)
	jsonResponse, err := json.Marshal(&resp)
	if err != nil {
		log.Error(err.Error())
		http.Error(w, err.Error(), http.StatusInternalServerError)
@@ -1217,7 +1351,6 @@ func geGetWithinRangeGeoDataByName(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/json; charset=UTF-8")
	w.WriteHeader(http.StatusOK)
	fmt.Fprint(w, string(jsonResponse))
	*/
}

func geGetGeoDataByName(w http.ResponseWriter, r *http.Request) {
+7 −1
Original line number Diff line number Diff line
@@ -28,5 +28,11 @@ package server
type DistanceResponse struct {

	// Distance between two points (in meters)
	Distance float32 `json:"distance,omitempty"`
	Distance float32 `json:"distance"`

	// Destination asset latitude
	Latitude float32 `json:"latitude,omitempty"`

	// Destination asset longitude
	Longitude float32 `json:"longitude,omitempty"`
}
+4 −4
Original line number Diff line number Diff line
@@ -27,12 +27,12 @@ package server
// Within range response
type WithinRangeResponse struct {

	// Asset latitude
	// Destination asset latitude
	Latitude float32 `json:"latitude,omitempty"`

	// Asset longitude
	Lontitude float32 `json:"lontitude,omitempty"`
	// Destination asset longitude
	Longitude float32 `json:"longitude,omitempty"`

	// Within range result (e.g. true = within range, false = beyond range)
	Within bool `json:"within,omitempty"`
	Within bool `json:"within"`
}
Loading