private NetTopologySuite.Features.AttributesTable AddAttribute(string jsonShape) { try { NetTopologySuite.Features.AttributesTable attributesTable = new NetTopologySuite.Features.AttributesTable(); if (IsValidJson(jsonShape)) { var obj = JObject.Parse(jsonShape); if (obj.Count > 0) { var features = obj["features"]; var properties = (JObject)features[0]["properties"]; attributesTable.AddAttribute("ObjectId", Convert.ToInt32(properties["ObjectId"].ToString())); attributesTable.AddAttribute("UUID", properties["UUID"].ToString()); attributesTable.AddAttribute("MaXa", properties["MaXa"].ToString()); attributesTable.AddAttribute("MaDoiTuong", properties["MaDoiTuong"].ToString()); attributesTable.AddAttribute("SoHieuTo", (properties["SoHieuToBanDo"] != null) ? Convert.ToInt32(properties["SoHieuToBanDo"].ToString()) : 0); attributesTable.AddAttribute("SoThua", (properties["SoThuTuThua"] != null) ? Convert.ToInt32(properties["SoThuTuThua"].ToString()) : 0); attributesTable.AddAttribute("DienTich", (properties["DienTich"] != null) ? Convert.ToDouble(properties["DienTich"].ToString()) : 0); attributesTable.AddAttribute("DTPhapLy", (properties["DienTichPhapLy"] != null) ? Convert.ToDouble(properties["DienTichPhapLy"].ToString()) : 0); attributesTable.AddAttribute("KyHieuMDSD", properties["KyHieuMucDichSuDung"].ToString()); attributesTable.AddAttribute("TenChu", properties["TenChu"].ToString()); attributesTable.AddAttribute("DiaChi", properties["DiaChi"].ToString()); attributesTable.AddAttribute("ShapeArea", (properties["ShapeArea"] != null && properties["ShapeArea"].ToString() != "") ? Convert.ToDouble(properties["ShapeArea"].ToString()) : 0); attributesTable.AddAttribute("Id", properties["Id"].ToString()); attributesTable.AddAttribute("TenChu2", properties["TenChu2"].ToString()); } } return(attributesTable); } catch (Exception ex) { return(null); } }
public bool ConvertJsonToShapeFileNew(string json, string shapeFilePath, string namefile) { string geometryString = ConvertGeometryToString(json); var attributesTable = new NetTopologySuite.Features.AttributesTable(); var attributes = AddAttribute(json); var geomFactory = new NetTopologySuite.Geometries.GeometryFactory(new NetTopologySuite.Geometries.PrecisionModel(), 4326); var wktReader = new WKTReader(geomFactory); var geometry = wktReader.Read(geometryString); string filesPathName = shapeFilePath.Substring(0, shapeFilePath.Length - 4); removeShapeFileIfExists(filesPathName); if (attributes != null) { attributesTable = attributes; var features = new List <NetTopologySuite.Features.IFeature> { new NetTopologySuite.Features.Feature(geometry, attributesTable) }; var name = namefile.Substring(0, namefile.Length - 4); // Create the directory where we will save the shapefile //var shapeFilePath = Path.Combine(Server.MapPath("~/Document"), name); if (!Directory.Exists(filesPathName)) { Directory.CreateDirectory(filesPathName); } // Construct the shapefile name. Don't add the .shp extension or the ShapefileDataWriter will // produce an unwanted shapefile var shapeFileName = Path.Combine(filesPathName, name); var shapeFilePrjName = Path.Combine(filesPathName, $"{name}.prj"); // Create the shapefile var outGeomFactory = NetTopologySuite.Geometries.GeometryFactory.Default; var writer = new ShapefileDataWriter(shapeFileName, outGeomFactory, Encoding.UTF8); var outDbaseHeader = ShapefileDataWriter.GetHeader(features[0], features.Count, Encoding.UTF8); writer.Header = outDbaseHeader; writer.Write(features); // Create the projection file using (var streamWriter = new StreamWriter(shapeFilePrjName)) { streamWriter.Write(GeographicCoordinateSystem.WGS84.WKT); } System.IO.File.WriteAllText(Path.Combine(filesPathName, $"{name}.cpg"), Encoding.UTF8.HeaderName); //var shapeFileReader = new ShapefileDataReader(shapeFileName, NetTopologySuite.Geometries.GeometryFactory.Default, Encoding.UTF8); //var read = shapeFileReader.Read(); //var values = new object[shapeFileReader.FieldCount - 1]; //var a = values[1]; //var a1 = shapeFileReader.GetName(0); //var geom = shapeFileReader.Geometry; string zipName = filesPathName + ".zip"; CompressToZipFile(new List <string>() { shapeFileName + ".shp", shapeFileName + ".dbf", shapeFileName + ".prj", shapeFileName + ".shx", shapeFileName + ".cpg" }, zipName); return(true); } return(false); }
private NetTopologySuite.Features.Feature getFeature(ReferenceGeometry g, GeomType type) { List <Coordinate> coordinatesSwissGrid = new List <Coordinate>(); Coordinate[] coordinates; if (type == GeomType.Point) { coordinates = g.Point.Coordinates; } else if (type == GeomType.Line) { coordinates = g.Line.Coordinates; } else if (type == GeomType.Polygon) { coordinates = g.Polygon.Coordinates; } else { coordinates = null; } foreach (Coordinate coor in coordinates) { // Convert Coordinates to LV03 double x = 0, y = 0, h = 0; CoordinateConverter.WGS84toLV03(coor.Y, coor.X, 0, ref x, ref y, ref h); Coordinate newcoord = new Coordinate(x, y); coordinatesSwissGrid.Add(newcoord); } try { var gf = new GeometryFactory(); Geometry polygonSwissGrid = null; if (type == GeomType.Point) { polygonSwissGrid = gf.CreatePoint(coordinatesSwissGrid[0]); } else if (type == GeomType.Line) { polygonSwissGrid = gf.CreateLineString(coordinatesSwissGrid.ToArray()); } else if (type == GeomType.Polygon) { polygonSwissGrid = gf.CreatePolygon(coordinatesSwissGrid.ToArray()); } NetTopologySuite.Features.AttributesTable attribute = new NetTopologySuite.Features.AttributesTable(); attribute.Add("id", g.GeometryId); attribute.Add("name", g.GeometryName); NetTopologySuite.Features.Feature i = new NetTopologySuite.Features.Feature(polygonSwissGrid, attribute); return(i); } catch (Exception e) { return(null); } }
private NetTopologySuite.Features.AttributesTable AddAttribute(Models.ShapeJsonViewModel.Properties properties) { try { NetTopologySuite.Features.AttributesTable attributesTable = new NetTopologySuite.Features.AttributesTable(); attributesTable.AddAttribute("ObjectId", properties.ObjectId); attributesTable.AddAttribute("Index", properties.Index); attributesTable.AddAttribute("UUID", properties.UUID != null ? properties.UUID : ""); attributesTable.AddAttribute("ThoiDiemBa", properties.ThoiDiemBatDau != null ? properties.ThoiDiemBatDau : ""); attributesTable.AddAttribute("ThoiDiemKe", properties.ThoiDiemKetThuc != null ? properties.ThoiDiemKetThuc : ""); attributesTable.AddAttribute("MaXa", properties.MaXa != null ? properties.MaXa : ""); attributesTable.AddAttribute("MaDoiTuong", properties.MaDoiTuong != null ? properties.MaDoiTuong : ""); attributesTable.AddAttribute("SoHieuToBa", properties.SoHieuToBanDo); attributesTable.AddAttribute("SoThuTuThu", properties.SoThuTuThua); attributesTable.AddAttribute("SoHieuTo_1", properties.SoHieuToBanDoCu != null ? properties.SoHieuToBanDoCu : ""); attributesTable.AddAttribute("SoThuTuT_1", properties.SoThuTuThuaCu != null ? properties.SoThuTuThuaCu : ""); attributesTable.AddAttribute("DienTich", properties.DienTich); attributesTable.AddAttribute("DienTichPh", properties.DienTichPhapLy); attributesTable.AddAttribute("KyHieuMucD", properties.KyHieuMucDichSuDung != null ? properties.KyHieuMucDichSuDung : ""); attributesTable.AddAttribute("KyHieuDoiT", properties.KyHieuDoiTuong != null ? properties.KyHieuDoiTuong : ""); attributesTable.AddAttribute("TenChu", properties.TenChu != null ? properties.TenChu : ""); attributesTable.AddAttribute("DiaChi", properties.DiaChi != null ? properties.DiaChi : ""); attributesTable.AddAttribute("DaCapGCN", properties.DaCapGCN); attributesTable.AddAttribute("TenChu2", properties.TenChu2 != null ? properties.TenChu2 : ""); attributesTable.AddAttribute("NamSinhC1", properties.NamSinhC1 != null ? properties.NamSinhC1 : ""); attributesTable.AddAttribute("SoHieuGCN", properties.SoHieuGCN != null ? properties.SoHieuGCN : ""); attributesTable.AddAttribute("SoVaoSo", properties.SoVaoSo != null ? properties.SoVaoSo : ""); attributesTable.AddAttribute("NgayVaoSo", properties.NgayVaoSo != null ? properties.NgayVaoSo : ""); attributesTable.AddAttribute("SoBienNhan", properties.SoBienNhan != null ? properties.SoBienNhan : ""); attributesTable.AddAttribute("NguoiNhanH", properties.NguoiNhanHS != null ? properties.NguoiNhanHS : ""); attributesTable.AddAttribute("CoQuanThuL", properties.CoQuanThuLy != null ? properties.CoQuanThuLy : ""); attributesTable.AddAttribute("LoaiHS", properties.LoaiHS != null ? properties.LoaiHS : ""); attributesTable.AddAttribute("MaLienKet", properties.MaLienKet != null ? properties.MaLienKet : ""); attributesTable.AddAttribute("ShapeSTAre", properties.ShapeSTArea != null ? properties.ShapeSTArea : ""); attributesTable.AddAttribute("ShapeSTLen", properties.ShapeSTLength != null ? properties.ShapeSTLength : ""); attributesTable.AddAttribute("ShapeLengt", properties.ShapeLength != null ? properties.ShapeLength : ""); attributesTable.AddAttribute("ShapeArea", properties.ShapeArea != null ? properties.ShapeArea : ""); attributesTable.AddAttribute("info", properties.info != null ? properties.info : ""); attributesTable.AddAttribute("Tags", properties.Tags != null ? properties.Tags : ""); attributesTable.AddAttribute("Id", properties.Id != null ? properties.Id : ""); attributesTable.AddAttribute("CreatedDat", properties.CreatedDate); attributesTable.AddAttribute("UpdatedDat", properties.UpdatedDate); attributesTable.AddAttribute("TenMucDich", properties.TenMucDichSuDung != null ? properties.TenMucDichSuDung : ""); return(attributesTable); } catch (Exception ex) { return(null); } }
/// <summary> /// Converts the instructions to features. /// </summary> public static NetTopologySuite.Features.FeatureCollection ToFeatures(this IEnumerable <Instruction> instructions, Route route) { var featureCollection = new NetTopologySuite.Features.FeatureCollection(); foreach (var instruction in instructions) { var attributes = new NetTopologySuite.Features.AttributesTable(); attributes.AddAttribute("text", instruction.Text); attributes.AddAttribute("type", instruction.Type); var location = route.Shape[instruction.Shape]; featureCollection.Add(new NetTopologySuite.Features.Feature( new NetTopologySuite.Geometries.Point(location.ToCoordinate()), attributes)); } return(featureCollection); }
/// <summary> /// Converts the given tree to a feature collection. /// </summary> public static NetTopologySuite.Features.FeatureCollection ToFeatureCollection(this Algorithms.Networks.Analytics.Trees.Models.Tree tree) { var featureCollection = new NetTopologySuite.Features.FeatureCollection(); foreach (var treeEdge in tree.Edges) { var attributes = new NetTopologySuite.Features.AttributesTable(); attributes.AddAttribute("weight1", treeEdge.Weight1.ToInvariantString()); attributes.AddAttribute("weight2", treeEdge.Weight2.ToInvariantString()); attributes.AddAttribute("edge", treeEdge.EdgeId.ToInvariantString()); attributes.AddAttribute("previous_edge", treeEdge.PreviousEdgeId.ToInvariantString()); featureCollection.Add(new NetTopologySuite.Features.Feature( treeEdge.ToLineString(), attributes)); } return(featureCollection); }