Esempio n. 1
1
 public static string ToGeoJson(System.Data.Entity.Spatial.DbGeometry location)
 {
     var wktReader = new WKTReader();
     var geometry = wktReader.Read(location.WellKnownValue.WellKnownText);
     var geoJsonWriter = new GeoJsonWriter();
     return geoJsonWriter.Write(geometry);
 }
Esempio n. 2
0
 public static string ToGeoJson(System.Data.Entity.Spatial.DbGeometry location, ProjectionInfo pStart,
     ProjectionInfo pEnd)
 {
     var wktReader = new WKTReader();
     var geometry = wktReader.Read(location.WellKnownValue.WellKnownText);
     geometry = Project(geometry, pStart, pEnd);
     var geoJsonWriter = new GeoJsonWriter();
     return geoJsonWriter.Write(geometry);
 }
 public async Task UpdateData(List<Feature> features, string index)
 {
     var writer = new GeoJsonWriter();
     var result = await _elasticClient.BulkAsync(bulk =>
     {
         foreach (var feature in features)
         {
             bulk.Index<object>(i => i.Index(index).Document(JsonConvert.DeserializeObject(writer.Write(feature))).Id(GetId(feature)));
         }
         return bulk;
     });
     if (result.IsValid == false)
     {
         result.ItemsWithErrors.ToList().ForEach(i => _logger.Error("Inserting " + i.Id + " falied with error: " + i.Error.Reason + " caused by: " + i.Error.CausedBy.Reason));
     }
 }
        public void GeoJsonDeserializeFeatureCollectionTest()
        {
            const string json = @"
{
  ""type"": ""FeatureCollection"",
  ""features"": [
    {
      ""type"": ""Feature"",
      ""geometry"": {
        ""type"": ""Point"",
        ""coordinates"": [
          1.0,
          2.0
        ]
      },
      ""properties"": {
        ""foo"": {
          ""bar"": ""xyz""
        }
      }
    }
  ]
}
";
            GeoJsonReader reader = new GeoJsonReader();
            FeatureCollection coll = reader.Read<FeatureCollection>(json);
            Assert.IsNotNull(coll);
            Assert.AreEqual(1, coll.Count);
            IFeature feature = coll[0];

            Assert.IsNotNull(feature);
            Assert.AreEqual(1, feature.Attributes.Count);

            var gjs = new GeoJsonWriter();
            gjs.Write(coll);
        }
Esempio n. 5
0
        /// <summary>
        /// Returns MarkerType property for legend.
        /// </summary>
        /// <param name="colorRepo"></param>
        /// <param name="resultDir"></param>
        /// <param name="fi"></param>
        /// <param name="resultName"></param>
        /// <returns></returns>
        private static void ProcessVectorFile(IColorRepository colorRepo, DirectoryInfo resultDir, FileInfo fi, string resultName)
        {
            string localFileName = fi.Name;
            string localResultName = resultName;

            Console.WriteLine(string.Format("Processing {0} into {1}...", localFileName, localResultName));
            StringBuilder bldr = new StringBuilder();

            NetTopologySuite.IO.ShapefileDataReader dataReader = new NetTopologySuite.IO.ShapefileDataReader(fi.FullName, new GeometryFactory());
            NetTopologySuite.Features.FeatureCollection featureCollection = new NetTopologySuite.Features.FeatureCollection();
            List<string> csvHdr = dataReader.DbaseHeader.Fields.Select(a => a.Name).ToList();
            csvHdr.Add(appColorNamspace);
            bldr.AppendLine(string.Join(",", csvHdr)); //write csv file header
            while (dataReader.Read())
            {
                NetTopologySuite.Features.Feature feature = new NetTopologySuite.Features.Feature();
                feature.Geometry = dataReader.Geometry;

                int numFields = dataReader.DbaseHeader.NumFields + 1;
                string[] keys = new string[numFields];
                int colorValueField = -1;
                for (int i = 0; i < numFields - 1; i++)
                {
                    keys[i] = dataReader.DbaseHeader.Fields[i].Name;
                    if (keys[i].Equals(colorRepo.ColorFieldForOutput(localFileName, localResultName)))
                    {
                        colorValueField = i;
                    }
                }

                //add attributes from source attribute table
                feature.Attributes = new AttributesTable();
                List<string> csvLine = new List<string>();
                for (int i = 0; i < numFields - 1; i++)
                {
                    object val = dataReader.GetValue(i);
                    feature.Attributes.AddAttribute(keys[i], val);
                    csvLine.Add(val.ToString());
                }

                if (colorRepo.MapColorsToThisResult(localFileName, localResultName))
                {
                    //mark outline colors in a different attribute than fill colors
                    string colorNs = colorRepo.IsOutlinedNotFilled(localFileName, localResultName) ? appOutlineNamespace : appColorNamspace;
                    keys[numFields - 1] = colorNs;

                    //add additional attribute for color binding
                    string hexClr = colorRepo.SingleColorForFile(localFileName, localResultName); //only path where colorValueField, i.e. ColorMap.clrField can be unpopulated.

                    if (string.IsNullOrEmpty(hexClr) && colorValueField > -1)
                    {
                        if (colorRepo.IsCategoricalMap(localFileName, resultName))
                        {
                            //categorical color map
                             hexClr = colorRepo.ColorsOfValueInFile(localFileName, localResultName, dataReader.GetString(colorValueField)).HexColor;
                        }
                        else
                        {
                            //numerical range color map
                            hexClr = colorRepo.ColorsOfValueInFile(localFileName, localResultName, dataReader.GetDouble(colorValueField)).HexColor;
                        }
                    }

                    if (string.IsNullOrEmpty(hexClr)) // else if (string.IsNullOrEmpty(hexClr) && colorValueField < 0)
                    {
                        throw new NotSupportedException("Cannot color a file with no attributes to bind to and no single-color given.");
                    }
                    csvLine.Add(hexClr);
                    feature.Attributes.AddAttribute(colorNs, hexClr);
                }

                bldr.AppendLine(string.Join(",", csvLine));
                featureCollection.Add(feature);
            }
            GeoJsonWriter wtr = new GeoJsonWriter();
            string layerJson = wtr.Write(featureCollection);

            File.WriteAllText(resultDir.FullName + localResultName, layerJson);
            File.WriteAllText(resultDir.FullName + localResultName.Replace(".json", ".csv"), bldr.ToString());
        }
Esempio n. 6
0
 public static string ToGeoJson(IEnumerable<System.Data.Entity.Spatial.DbGeometry> dbGeometrys,
     ProjectionInfo pStart, DataTable dataTable)
 {
     var pEnd = Definitions.WorldProjection;
     var dbGeometrys1 = dbGeometrys as IList<System.Data.Entity.Spatial.DbGeometry> ?? dbGeometrys.ToList();
     var reader = new WKTReader();
     var featureCollection = new FeatureCollection();
     var columns = (from DataColumn column in dataTable.Columns select column.ColumnName).ToList();
     for (var i = 0; i < dbGeometrys1.Count(); i++)
     {
         var geometry = GeometryHelper.Project(dbGeometrys1[i].MakeValid(), pStart, pEnd);
         var read = reader.Read(geometry.WellKnownValue.WellKnownText);
         var table = new AttributesTable();
         foreach (var column in columns)
         {
             table.AddAttribute(column, dataTable.Rows[i][column]);
         }
         featureCollection.Add(new Feature(read, table));
     }
     var geoJsonWriter = new GeoJsonWriter();
     return geoJsonWriter.Write(featureCollection);
 }
Esempio n. 7
0
 public static string ToGeoJson(IEnumerable<System.Data.Entity.Spatial.DbGeometry> dbGeometrys,
     ProjectionInfo pStart)
 {
     var pEnd = Definitions.WorldProjection;
     var enumerable = dbGeometrys as IList<System.Data.Entity.Spatial.DbGeometry> ?? dbGeometrys.ToList();
     var reader = new WKTReader();
     var geometryCollection =
         new GeometryCollection(
             enumerable.Select(x => GeometryHelper.Project(x.MakeValid(), pStart, pEnd))
                 .Select(dbGeometry => reader.Read(dbGeometry.WellKnownValue.WellKnownText))
                 .ToArray());
     var geoJsonWriter = new GeoJsonWriter();
     return geoJsonWriter.Write(geometryCollection);
 }
        public void geojson_should_serialize_an_array_witn_a_single_item()
        {
            const string json = @"
{
  ""type"": ""FeatureCollection"",
  ""features"": [
    {
      ""type"": ""Feature"",
      ""geometry"": {
        ""type"": ""Point"",
        ""coordinates"": [
          1.0,
          2.0
        ]
      },
      ""properties"": {
        ""foo"": [
            {
              ""zee1"": ""xyz1""
            }
          ]
      }
    }
  ]
}
";
            GeoJsonReader reader = new GeoJsonReader();
            FeatureCollection coll = reader.Read<FeatureCollection>(json);
            Assert.IsNotNull(coll);

            GeoJsonWriter writer = new GeoJsonWriter();
            string s = writer.Write(coll);
            Assert.IsNotNull(s);
        }