Example #1
0
        public IEnumerable <GeoData> ParseGeodataFromSource(LayersConfiguration sourceData)
        {
            string filePath = sourceData.Source;

            var reader = new ShapefileDataReader(filePath, new GeometryFactory());

            while (reader.Read())
            {
                var geoData = ConvertReaderToGeoData(reader);

                if (sourceData.Filters == null || sourceData.Filters.Length == 0)
                {
                    yield return(geoData);
                }
                else
                {
                    foreach (var filter in sourceData.Filters)
                    {
                        if (!geoData.Values.ContainsKey(filter.Field))
                        {
                            throw new Exception($"Field {filter.Field} was not found on shapefile {filePath}");
                        }

                        if (Convert.ToString(geoData.Values[filter.Field]) == filter.Value)
                        {
                            yield return(geoData);
                        }
                    }
                }
            }
        }
Example #2
0
        public IEnumerable <GeoData> ParseGeodataFromSource(LayersConfiguration sourceData)
        {
            string filePath = sourceData.Source;

            var jsonData = File.ReadAllText(filePath);

            var reader = new GeoJsonReader();

            var featureCollection = reader.Read <FeatureCollection>(jsonData);

            return(featureCollection.Features.Select(ConvertFeatureToGeoData));
        }
Example #3
0
        public IEnumerable <GeoData> ParseGeodataFromSource(LayersConfiguration sourceData)
        {
            string filePath = sourceData.Source;

            foreach (var line in File.ReadLines(filePath))
            {
                if (line == null)
                {
                    continue;
                }

                var components = line.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                var longitude = double.Parse(components[0], new CultureInfo("en-US"));
                var latitude  = double.Parse(components[1], new CultureInfo("en-US"));

                var geodata = new GeoData
                {
                    Values = new Dictionary <string, object>(StringComparer.InvariantCultureIgnoreCase),
                    Points = new PointXY[1][]
                };

                if (Math.Abs(sourceData.XOffset) > (decimal)0.0 && Math.Abs(sourceData.YOffset) > (decimal)0.0)
                {
                    geodata.Points[0] = new[]
                    {
                        new PointXY(longitude - (double)sourceData.XOffset,
                                    latitude - (double)sourceData.YOffset), //TOP-LEFT

                        new PointXY(longitude + (double)sourceData.XOffset,
                                    latitude + (double)sourceData.YOffset) //BOTTOM-RIGHT
                    }
                }
                ;
                else
                {
                    geodata.Points[0] = new[] { new PointXY(longitude, latitude) };
                }

                var values = components.Skip(2);

                var fieldCount = 0;
                foreach (var value in values)
                {
                    geodata.Values.Add("custom_" + fieldCount++, value);
                }

                yield return(geodata);
            }
        }
Example #4
0
        internal static void Process(GeoDataToHexagonDataArgs opts)
        {
            Stopwatch           stopwatch     = Stopwatch.StartNew();
            LayersConfiguration layer         = opts.GetLayersConfiguration();
            MergeStrategy       mergeStrategy = opts.Merge;

            ValueHandlerFactory valueHandlerFactory = new ValueHandlerFactory();

            valueHandlerFactory.RegisterImplementation <WikimediaAltitudeHandler>("wikimedia_altitude");

            HexagonDefinition hexagonDefinition = opts.GetHexagonDefinition();
            HexagonProcessor  hexagonProcessor  = new HexagonProcessor(hexagonDefinition, valueHandlerFactory);

            BoundingBox bb = null;

            if (!string.IsNullOrEmpty(opts.East) && !string.IsNullOrEmpty(opts.West) &&
                !string.IsNullOrEmpty(opts.North) && !string.IsNullOrEmpty(opts.South))
            {
                bb = new BoundingBox
                {
                    East  = double.Parse(opts.East),
                    West  = double.Parse(opts.West),
                    North = double.Parse(opts.North),
                    South = double.Parse(opts.South)
                };
            }

            using (IGeoDataParser geoParser = opts.GetGeoDataParser())
                using (IHexagonDataExporter exporter = opts.GetResultExporter())
                {
                    IEnumerable <GeoData> geoDataList = geoParser.ParseGeodataFromSource(layer);

                    IEnumerable <Hexagon> results = hexagonProcessor.ProcessHexagonsFromGeoData(geoDataList, layer.Targets, bb);

                    ExportResults(exporter, results, hexagonDefinition, mergeStrategy);
                }

            stopwatch.Stop();

            Console.WriteLine($"Process took {stopwatch.ElapsedMilliseconds} ms");
        }
Example #5
0
        /// <summary>
        ///     Information obtained from: http://duff.ess.washington.edu/data/raster/drg/docs/geotiff.txt
        /// </summary>
        /// <param name="sourceData"></param>
        /// <returns></returns>
        public IEnumerable <GeoData> ParseGeodataFromSource(LayersConfiguration sourceData)
        {
            string filePath = sourceData.Source;

            Tiff.SetErrorHandler(new NoWarningsTiffErrorHandler());
            using (var tiff = Tiff.Open(filePath, "r"))
            {
                var modelPixelScaleTag = tiff.GetField(TiffTag.GEOTIFF_MODELPIXELSCALETAG);
                var modelTiepointTag   = tiff.GetField(TiffTag.GEOTIFF_MODELTIEPOINTTAG);

                var modelPixelScale = modelPixelScaleTag[1].GetBytes();
                var pixelSizeX      = BitConverter.ToDouble(modelPixelScale, 0);
                var pixelSizeY      = BitConverter.ToDouble(modelPixelScale, 8) * -1;

                var modelTransformation = modelTiepointTag[1].GetBytes();
                var originLon           = BitConverter.ToDouble(modelTransformation, 24);
                var originLat           = BitConverter.ToDouble(modelTransformation, 32);

                var startLat = originLat + pixelSizeY / 2.0;
                var startLon = originLon + pixelSizeX / 2.0;


                //TODO: Check if band is stored in 1 byte or 2 bytes. If 2, the following code would be required
                //var scanline16Bit = new ushort[tiff.ScanlineSize() / 2];
                //Buffer.BlockCopy(scanline, 0, scanline16Bit, 0, scanline.Length);

                var currentLat = startLat;
                var currentLon = startLon;

                var imageLength = tiff.GetField(TiffTag.IMAGELENGTH)[0].ToInt();
                var buf         = new byte[tiff.ScanlineSize()];

                for (var i = 0; i < imageLength; i++)
                {
                    tiff.ReadScanline(buf, i); //Loading ith Line

                    var latitude = currentLat + pixelSizeY * i;

                    for (var j = 0; j < buf.Length; j++)
                    {
                        var longitude = currentLon + pixelSizeX * j;

                        var geodata = new GeoData
                        {
                            Values = new Dictionary <string, object>(StringComparer.InvariantCultureIgnoreCase),
                            Points = new PointXY[1][]
                        };

                        if (sourceData.Interpolate)
                        {
                            geodata.Points[0] = new[]
                            {
                                new PointXY(longitude - pixelSizeX, latitude - pixelSizeY), //TOP-LEFT
                                new PointXY(longitude + pixelSizeX, latitude + pixelSizeY)  //BOTTOM-RIGHT
                            }
                        }
                        ;
                        else
                        {
                            geodata.Points[0] = new[] { new PointXY(longitude, latitude) };
                        }

                        geodata.DataType = DataType.Pixel;
                        geodata.Values.Add("custom_1", buf[j]);

                        yield return(geodata);
                    }
                }
            }
        }
Example #6
0
        internal LayersConfiguration GetLayersConfiguration()
        {
            var targets = new List <LayersLoaderTarget>();

            foreach (string target in Targets)
            {
                string[] lines = target.Split(":");

                //country:iso_a2
                //river
                //altitude:handler(wikimedia_altitude)

                LayersLoaderTarget loaderTarget = new LayersLoaderTarget
                {
                    Destination = lines[0]
                };


                if (lines.Length == 1)
                {
                    loaderTarget.SourceField = lines[0];
                }
                else
                {
                    Regex regex = new Regex("handler\\((?<handler>\\w+)\\)");
                    Match m     = regex.Match(lines[1]);

                    if (m.Success)
                    {
                        loaderTarget.Handler = m.Groups["handler"].Value;
                    }
                    else
                    {
                        loaderTarget.SourceField = lines[1];
                    }
                }

                targets.Add(loaderTarget);
            }

            var filters = new List <LayersLoaderFilter>();

            foreach (string filter in Filters)
            {
                string[] lines = filter.Split(":");

                if (lines.Length != 2)
                {
                    throw new NotSupportedException($"A filter needs 2 parts: <field>:<value-to-include>");
                }

                filters.Add(new LayersLoaderFilter {
                    Field = lines[0], Value = lines[1]
                });
            }

            var layers = new LayersConfiguration
            {
                Source      = Input,
                Targets     = targets.ToArray(),
                Filters     = filters.ToArray(),
                Interpolate = Interpolate
            };

            return(layers);
        }