照片提取GPS 转成百度地图坐标

时间:2023-03-08 16:30:33
照片提取GPS 转成百度地图坐标

感谢: 小慧only http://www.cnblogs.com/zhaohuionly/p/3142623.html  GPS转化坐标方法
   大胡子青松 http://www.cnblogs.com/xianyin05/archive/2013/05/10/3071224.html  获取照片中Exif信息里GPS经纬度数

需求:上传个图片 解释出拍摄地的 GPS 信息,以地图的形式展现出来。GPS 信息的验证,可下载 MagicEXIF 元数据编辑器 查看

不多说,一下为代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="Code.Test.WebForm2" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="file" id="file_img" runat="server"/>
<asp:Button runat="server" Text="提取" ID="btn_zh" OnClick="btn_zh_Click"/>
<asp:Image runat="server" ID="Map" />
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;
using System.IO;
using System.Drawing.Imaging;
using System.Net;
using Newtonsoft.Json; namespace Code.Test
{
public partial class WebForm2 : System.Web.UI.Page
{
Code.SysManage.Commen.CommonTool commonTool = new SysManage.Commen.CommonTool();
public class MapConvert
{
public string error { get; set; }
public string x { get; set; }
public string y { get; set; }
}
protected void Page_Load(object sender, EventArgs e)
{ } #region 读取图片中GPS点 /// <summary>
/// 获取图片中的GPS坐标点
/// </summary>
/// <param name="p_图片路径">图片路径</param>
/// <returns>返回坐标【纬度+经度】用"+"分割 取数组中第0和1个位置的值</returns>
public String fnGPS坐标(String p_图片路径)
{
String s_GPS坐标 = "";
//载入图片
System.Drawing.Image objImage = System.Drawing.Image.FromFile(p_图片路径);
//取得所有的属性(以PropertyId做排序)
var propertyItems = objImage.PropertyItems.OrderBy(x => x.Id);
//暂定纬度N(北纬)
char chrGPSLatitudeRef = 'N';
//暂定经度为E(东经)
char chrGPSLongitudeRef = 'E';
foreach (PropertyItem objItem in propertyItems)
{
//只取Id范围为0x0000到0x001e
if (objItem.Id >= 0x0000 && objItem.Id <= 0x001e)
{
switch (objItem.Id)
{
case 0x0000:
var query = from tmpb in objItem.Value select tmpb.ToString();
string sreVersion = string.Join(".", query.ToArray());
break;
case 0x0001://北纬 N chrGPSLatitudeRef
chrGPSLatitudeRef = BitConverter.ToChar(objItem.Value, );
break;
case 0x0002://维度 strLatitude
if (objItem.Value.Length == )
{
//degrees(将byte[0]~byte[3]转成uint, 除以byte[4]~byte[7]转成的uint)
double d = BitConverter.ToUInt32(objItem.Value, ) * 1.0d / BitConverter.ToUInt32(objItem.Value, );
//minutes(將byte[8]~byte[11]转成uint, 除以byte[12]~byte[15]转成的uint)
double m = BitConverter.ToUInt32(objItem.Value, ) * 1.0d / BitConverter.ToUInt32(objItem.Value, );
//seconds(將byte[16]~byte[19]转成uint, 除以byte[20]~byte[23]转成的uint)
double s = BitConverter.ToUInt32(objItem.Value, ) * 1.0d / BitConverter.ToUInt32(objItem.Value, );
//计算经纬度数值, 如果是南纬, 要乘上(-1)
double dblGPSLatitude = (((s / + m) / ) + d) * (chrGPSLatitudeRef.Equals('N') ? : -);
string strLatitude = string.Format("{0:#}°{1:#}'{2:#.00}\"{3}", d
, m, s, chrGPSLatitudeRef);
//纬度
//s_GPS坐标 += "维度:" + dblGPSLatitude + "," + strLatitude + "|";
s_GPS坐标 += (dblGPSLatitude + "|");
}
break;
case 0x0003: //东经 chrGPSLongitudeRef
//透过BitConverter, 将Value转成Char('E' / 'W')
//此值在后续的Longitude计算上会用到
chrGPSLongitudeRef = BitConverter.ToChar(objItem.Value, );
break;
case 0x0004: //经度
if (objItem.Value.Length == )
{
//degrees(将byte[0]~byte[3]转成uint, 除以byte[4]~byte[7]转成的uint)
double d = BitConverter.ToUInt32(objItem.Value, ) * 1.0d / BitConverter.ToUInt32(objItem.Value, );
//minutes(将byte[8]~byte[11]转成uint, 除以byte[12]~byte[15]转成的uint)
double m = BitConverter.ToUInt32(objItem.Value, ) * 1.0d / BitConverter.ToUInt32(objItem.Value, );
//seconds(将byte[16]~byte[19]转成uint, 除以byte[20]~byte[23]转成的uint)
double s = BitConverter.ToUInt32(objItem.Value, ) * 1.0d / BitConverter.ToUInt32(objItem.Value, );
//计算精度的数值, 如果是西经, 要乘上(-1)
double dblGPSLongitude = (((s / + m) / ) + d) * (chrGPSLongitudeRef.Equals('E') ? : -);
string strLongitude = string.Format("{0:#}°{1:#}'{2:#.00}\"{3}", d
, m, s, chrGPSLongitudeRef); //s_GPS坐标 += "经度:" + dblGPSLongitude + "," + strLongitude + "|";
s_GPS坐标 += (dblGPSLongitude + "|");
}
break;
case 0x0005://海平面
string strAltitude = BitConverter.ToBoolean(objItem.Value, ) ? "" : "";
s_GPS坐标 += "海平面:" + strAltitude + "|";
break;
case 0x0006://高度
if (objItem.Value.Length == )
{
//将byte[0]~byte[3]转成uint, 除以byte[4]~byte[7]转成的uint
double dblAltitude = BitConverter.ToUInt32(objItem.Value, ) * 1.0d / BitConverter.ToUInt32(objItem.Value, ); s_GPS坐标 += "水平高度:" + dblAltitude + "|";
}
break;
}
}
}
return s_GPS坐标;
} #endregion protected void btn_zh_Click(object sender, EventArgs e)
{ string picpath = commonTool.SaveImg(file_img, "News"); /*上传图片的方法,可自己重写*/
picpath = HttpContext.Current.Request.MapPath(picpath);
string Gpslocation = fnGPS坐标(picpath);/*获得GPS 信心,详细信息参考方法*/
Response.Write(Gpslocation);
//获取当前纬度
string Latitude = Gpslocation.Split('|')[];/*上传图片的方法,可自己重写*/ //获取当前经度
string Longitude = Gpslocation.Split('|')[]; //百度坐标转换API
string path = "http://api.map.baidu.com/ag/coord/convert?from=0&to=4&x=" + Longitude + "&y=" + Latitude + "";/*坐标纠正,生成百度地图的坐标*/ //WebClient请求
WebClient wc = new WebClient();
Stream stream = wc.OpenRead(path);
StreamReader sr = new StreamReader(stream);
MapConvert mapConvert = new MapConvert();
string strResult = sr.ReadToEnd();
Response.Write(strResult);
mapConvert = JsonConvert.DeserializeObject<MapConvert>(strResult);
string lon = mapConvert.x;
string lat = mapConvert.y;
//进行Base64解码
byte[] xBuffer = Convert.FromBase64String(lon);
string strX = Encoding.UTF8.GetString(xBuffer, , xBuffer.Length);
byte[] yBuffer = Convert.FromBase64String(lat);
string strY = Encoding.UTF8.GetString(yBuffer, , xBuffer.Length);
Response.Write(strX + ",");
Response.Write(strY);
//生成静态图片
string imgSrc = string.Format("http://api.map.baidu.com/staticimage?center={0},{1}&width=600&height=600&zoom=19&markers={2},{3}&markerStyles=l,A", strX, strY, strX, strY);/*此处生成静态地图,以显示是否正确,可自行修改*/ //显示图片
Map.ImageUrl = imgSrc;
}
} }