KML ファイルからのデータを保存しています。何らかの価値を持たせるために、この方法でデータベースを作成しました。
create table PONTO
(
PontoID int not null identity,
ClienteID int null,
Descricao varchar(255) not null,
Latitude numeric(18,15) not null,
Longitude numeric(18,15) not null,
Altura decimal(12,2) null
)
alter table PONTO add constraint PK_Ponto primary key (PontoID)
create table LINK
(
LinkID int not null identity,
PontoOrigemID int null,
PontoDestinoID int null,
LatitudeOrigem numeric(18,15) not null,
LongitudeOrigem numeric(18,15) not null,
LatitudeDestino numeric(18,15) not null,
LongitudeDestino numeric(18,15) not null,
Descricao varchar(255) not null,
Detalhes text null,
Altura decimal(12, 2) null,
Distancia decimal(12, 3) null,
AzimuteOrigemDestino decimal(3, 2) null,
AzimuteDestinoOrigem decimal(3, 2) null,
Velocidade varchar(50) null
)
alter table LINK add constraint PK_Link primary key (LinkID)
フロートを作成しようとしましたが、参加しようとするとうまくいきませんでした。
データを保存するための私のクラスは次のとおりです。
public class Ponto
{
public int PontoID { get; set; }
public int? ClienteID { get; set; }
public string Descricao { get; set; }
public Double Latitude { get; set; }
public Double Longitude { get; set; }
public Double? Altura { get; set; }
}
public class Link
{
[Key]
public int LinkID { get; set; }
public int? PontoOrigemID { get; set; }
public int? PontoDestinoID { get; set; }
public Double LatitudeOrigem { get; set; }
public Double LongitudeOrigem { get; set; }
public Double LatitudeDestino { get; set; }
public Double LongitudeDestino { get; set; }
public string Descricao { get; set; }
public string Detalhes { get; set; }
public Double? Altura { get; set; }
public Double? Distancia { get; set; }
public Double? AzimuteOrigemDestino { get; set; }
public Double? AzimuteDestinoOrigem { get; set; }
public string Velocidade { get; set; }
}
ファイル KML を保存:
public ActionResult CarregarArquivo()
{
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath(@"~\AppData\doc.xml"));
var dados = new List<Ponto>();
Ponto ponto;
Link link;
/* Existe duas pastas folder dentro do arquivo, a primeira com os pontos,
segunda com os links */
foreach (XmlNode nodePrincipal in doc.SelectNodes("/kml/Document/Folder"))
{
/* Pontos definidos */
foreach (XmlNode nodePontos in nodePrincipal.SelectNodes("Placemark"))
{
ponto = new Ponto();
ponto.Descricao = nodePontos.SelectSingleNode("name").InnerText; /* Nome do ponto */
var coord = nodePontos.SelectSingleNode("Point/coordinates").InnerText.Split(',');
ponto.Longitude = Convert.ToDouble(coord[0].Replace('.', ','));
ponto.Latitude = Convert.ToDouble(coord[1].Replace('.', ','));
using (var db = new TCC.Models.ERPContext())
{
db.Ponto.Add(ponto);
db.SaveChanges();
}
}
/* Verifica se tem outra pasta folder, que indica que é uma pasta de links */
foreach (XmlNode nodeEnlace in nodePrincipal.SelectNodes("Folder"))
{
link = new Link();
link.Descricao = nodeEnlace.SelectSingleNode("name").InnerText;
foreach (XmlNode nodeEnlacePonto in nodeEnlace.SelectNodes("Placemark"))
{
if (nodeEnlacePonto.Attributes["id"] != null)
{
if (link.LatitudeOrigem == 0.0 && link.LongitudeOrigem == 0.0)
{
link.LatitudeOrigem = Convert.ToDouble(nodeEnlacePonto.SelectSingleNode("LookAt/latitude").InnerText.Replace('.', ','));
link.LongitudeOrigem = Convert.ToDouble(nodeEnlacePonto.SelectSingleNode("LookAt/longitude").InnerText.Replace('.', ','));
}
else
{
link.LatitudeDestino = Convert.ToDouble(nodeEnlacePonto.SelectSingleNode("LookAt/latitude").InnerText.Replace('.', ','));
link.LongitudeDestino = Convert.ToDouble(nodeEnlacePonto.SelectSingleNode("LookAt/longitude").InnerText.Replace('.', ','));
}
}
}
using (var db = new TCC.Models.ERPContext())
{
db.Link.Add(link);
db.SaveChanges();
}
}
}
return RedirectToAction("Index");
}
問題は、データを取得しようとすると、システムがエラーを返すことです。
'Point' のプロパティ 'Latitude' を値 'Decimal' で設定できません。このプロパティは、'Double' 型の null 以外の値に設定する必要があります。
リストするには:
public JsonResult CarregaDados()
{
using (var db = new ERPContext())
{
var dados = db.Ponto.ToList();
return Json(dados.ToArray(), JsonRequestBehavior.AllowGet);
}
}
プロセスを改善して機能させるにはどうすればよいですか?
VS2008、SQL Server 2008、C#、および Entity Framework を使用しています。