/// <summary> /// Retrieves the bounds as points. /// </summary> /// <param name="topLeftPoint">The point which will represent the top-left of the bounds</param> /// <param name="bottomRightPoint">The point which will represent the bottom-right of the bounds</param> public void GetBounds(PostGisPoint topLeftPoint, PostGisPoint bottomRightPoint) { // Ensure that the database connection is open if (mConnection.FullState != ConnectionState.Open) { return; } // Error catching try { // Initialize the command using NpgsqlCommand command = new NpgsqlCommand( "SELECT name, ST_AsText(ST_Transform(geom, 3857)) AS point FROM bounds;", mConnection); // Execute the command and start reading the results using NpgsqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { // Get the string representation of the name and point string name = reader.GetString(reader.GetOrdinal("name")); string point = reader.GetString(reader.GetOrdinal("point")); // Check which point was read if (name.Equals("top_left")) { // Parse top left point topLeftPoint.X = double.Parse(point[(point.IndexOf("(") + 1)..point.IndexOf(" ")]);
/// <summary> /// Inserts the PNG into the Database. /// </summary> /// <param name="rotation">The rotation of the image</param> /// <param name="imagePath">Path of where the image is stored.</param> /// <param name="name">Name of the image</param> /// <param name="centerPoint">The PostGIS data point of the center of the image</param> /// <param name="widthInKm">The width of the image in kilometers</param> /// <param name="heightInKm">The height of the image in kilometers</param> public void InsertPng( double rotation, string imagePath, string name, PostGisPoint centerPoint, double widthInKm, double heightInKm) { // Get the bytes from the PNG file and convert to hex string byte[] imageBytes = File.ReadAllBytes(FileIO.GetOutputDirectory() + imagePath); string imageHex = string.Concat(imageBytes.Select(b => b.ToString("X2")).ToArray()); // Create the JSON object JsonObject obj = new JsonObject { image_name = name, image_data = imageHex, image_size = imageHex.Length, center_point = $"POINT({centerPoint.Longitude} {centerPoint.Latitude})", km_height = heightInKm, km_width = widthInKm, image_rotation = rotation }; // Serialize for the message string jsonString = JsonSerializer.Serialize(obj); // Create the API request HttpWebRequest request = (HttpWebRequest)WebRequest.Create(ConfigInterface.ConnectionApi.AddImageUrl); request.ContentType = "application/json; charset=utf-8"; request.Method = "POST"; // Prepare the message byte[] message = new ASCIIEncoding().GetBytes(jsonString); request.ContentLength = message.Length; Stream stream = request.GetRequestStream(); // Send the message stream.Write(message, 0, message.Length); try { // Retrieve the API response HttpWebResponse response = (HttpWebResponse)request.GetResponse(); if (response.StatusCode != HttpStatusCode.OK) { MessageBox.Show(string.Format(Application.Current.FindResource("PromptApiWebResponseProblem")?.ToString(), response.StatusDescription)); } } catch (WebException e) { MessageBox.Show(string.Format(Application.Current.FindResource("PromptApiWebException")?.ToString(), e.Message)); } MessageBox.Show(Application.Current.FindResource("PromptApiImageInsertedSuccessfully")?.ToString()); }
/// <summary> /// Returns the distance in miles or kilometers of any two /// latitude / longitude points. /// </summary> /// <param name="pos1">Location 1</param> /// <param name="pos2">Location 2</param> /// <param name="unit">Miles or Kilometers</param> /// <returns>Distance in the requested unit</returns> public double HaversineDistance(PostGisPoint pos1, PostGisPoint pos2, DistanceUnit unit) { double R = (unit == DistanceUnit.Miles) ? 3960 : 6371; var lat = (pos2.Latitude - pos1.Latitude).ToRadians(); var lng = (pos2.Longitude - pos1.Longitude).ToRadians(); var h1 = Math.Sin(lat / 2) * Math.Sin(lat / 2) + Math.Cos(pos1.Latitude.ToRadians()) * Math.Cos(pos2.Latitude.ToRadians()) * Math.Sin(lng / 2) * Math.Sin(lng / 2); var h2 = 2 * Math.Asin(Math.Min(1, Math.Sqrt(h1))); return(R * h2); }
/// <summary> /// This method will be called from a button click to save the historical map / historical map data to the DB /// </summary> public void SaveMap() { // Unity coords for BaseMap Vector2 basemapPosition = new Vector2(BaseMap.transform.position.x, BaseMap.transform.position.z); Vector2 unityTopLeftCorner = new Vector2(basemapPosition.x - 5, basemapPosition.y + (5 * BaseMap.transform.localScale.z)); Vector2 unityTopRightCorner = new Vector2(basemapPosition.x + 5, basemapPosition.y + (5 * BaseMap.transform.localScale.z)); Vector2 unityBotLeftCorner = new Vector2(basemapPosition.x - 5, basemapPosition.y - (5 * BaseMap.transform.localScale.z)); Vector2 unityBotRightCorner = new Vector2(basemapPosition.x + 5, basemapPosition.y - (5 * BaseMap.transform.localScale.z)); // Unity coord for center of OverlayMap Vector2 unityCenterPoint = new Vector2(OverlayMap.transform.position.x, OverlayMap.transform.position.z); // Basemap GIS info double baseMapWidthInGIS = GISTopRightCorner.x - GISTopLeftCorner.x; double baseMapWidthInUnity = unityTopRightCorner.x - unityTopLeftCorner.x; double baseMapHeightInGIS = GISTopLeftCorner.y - GISBotLeftCorner.y; double baseMapHeightInUnity = unityTopLeftCorner.y - unityBotLeftCorner.y; // ratio for basemap calculation double ratioX = baseMapWidthInGIS / baseMapWidthInUnity; double ratioY = baseMapHeightInGIS / baseMapHeightInUnity; // get unity coord offsets for overlay map double overlayMapOffsetX = unityCenterPoint.x - unityTopLeftCorner.x; double overlayMapOffsetY = unityCenterPoint.y - unityBotLeftCorner.y; // get GIS coords of overlay map double overlayMapXInGIS = (overlayMapOffsetX * ratioX) + GISTopLeftCorner.x; double overlayMapYInGIS = (overlayMapOffsetY * ratioY) + GISBotLeftCorner.y; string center_point = "POINT(" + overlayMapXInGIS + " " + overlayMapYInGIS + ")"; // get rotation of overlay map float rotation = slider.value * 360; // get distances for BaseMap dimensions PostGisPoint topLeft = new PostGisPoint(GISTopLeftCorner); PostGisPoint topRight = new PostGisPoint(GISTopRightCorner); PostGisPoint botLeft = new PostGisPoint(GISBotLeftCorner); PostGisPoint botRight = new PostGisPoint(GISBotRightCorner); double baseMapWidthInKM = HaversineDistance(topLeft, topRight, DistanceUnit.Kilometers); double baseMapHeightInKM = HaversineDistance(topLeft, botLeft, DistanceUnit.Kilometers); // get pixels per KM ratio double horizontalPPK = baseMapWidthInKM / baseMapWidthInUnity; double verticalPPK = baseMapHeightInKM / baseMapHeightInUnity; // get pixel counts for OverlayMap double oMapPxlLen = 10; double histMapPixelWidth = oMapPxlLen * OverlayMap.transform.localScale.x; double histMapPixelHeight = oMapPxlLen * OverlayMap.transform.localScale.z; // get KM lnegths of the histrical map's sides double histMapWidthKM = histMapPixelWidth * horizontalPPK; double histMapHeightKM = histMapPixelHeight * verticalPPK; // get the image data into hex array string filePath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\histMap.png"; byte[] imgData = File.ReadAllBytes(filePath); string imageHex = string.Concat(imgData.Select(b => b.ToString("X2")).ToArray()); /// TODO do the api insert call... we have the info!!! /// km_width = histMapWidthKM (double variable) /// km_height = histMapHeightKM (double variable) /// rotation = rotation (float variable) /// Center point = 'POINT(overlayMapXInGIS overlayMapYInGIS)' (both are double variables) /// img_data = imageHex (string variable) ApiPostImage(center_point, imageHex, rotation, histMapHeightKM, histMapWidthKM); }