private Feature ToFeature(Area area)
        {
            var postGisReader = new PostGisReader();
            var attributes    = new AttributesTable();

            attributes.Add("id", area.AreaId);
            attributes.Add("parent_id", area.ParentAreaId);

            if (area.AreaStatistics != null)
            {
                foreach (var at in area.AreaStatistics)
                {
                    if (attributes.Exists(at.Key))
                    {
                        continue;
                    }

                    attributes.Add(at.Key, at.Value);
                }
            }

            if (!attributes.Exists(Constants.StatisticKeyCount))
            {
                attributes.Add(Constants.StatisticKeyCount,
                               Random.Next(10000));
            }
            if (!attributes.Exists(Constants.StatisticKeyMeter))
            {
                attributes.Add(Constants.StatisticKeyMeter,
                               Random.Next(10000));
            }
            if (!attributes.Exists(Constants.StatisticKeyTime))
            {
                attributes.Add(Constants.StatisticKeyTime,
                               Random.Next(10000));
            }

            if (area.AreaAttributes != null)
            {
                foreach (var at in area.AreaAttributes)
                {
                    if (attributes.Exists(at.Key))
                    {
                        continue;
                    }

                    attributes.Add(at.Key, at.Value);
                }
            }

            var geometry = postGisReader.Read(area.Geometry);

            return(new Feature(geometry, attributes));
        }
Esempio n. 2
0
        public static IAttributesTable ReadAttributesTable(JsonTextReader jreader)
        {
            if (jreader == null)
            {
                throw new ArgumentNullException("jreader", "A valid JSON reader object is required.");
            }

            IAttributesTable attributes = null;

            if (jreader.MoveToContent() && jreader.TokenClass == JsonTokenClass.Object)
            {
                attributes = new AttributesTable();
                jreader.ReadToken(JsonTokenClass.Object);

                while (jreader.TokenClass == JsonTokenClass.Member)
                {
                    string key   = jreader.ReadMember();
                    string value = jreader.ReadString();

                    if (!attributes.Exists(key))
                    {
                        attributes.AddAttribute(key, null);
                    }
                    attributes[key] = value;
                }

                jreader.ReadToken(JsonTokenClass.EndObject);
            }
            return(attributes);
        }
Esempio n. 3
0
        private static void AssertEqual(Dictionary <string, object> expected, AttributesTable actual)
        {
            // test everything after a serialize + deserialize round-trip
            ExpectedAndActual.RoundTrip(ref expected, ref actual);

            Assert.That(actual.Count, Is.EqualTo(expected.Count));
            Assert.That(actual, Is.EquivalentTo(expected));

            Assert.That(actual.GetNames().Count, Is.EqualTo(expected.Keys.Count));
            Assert.That(actual.GetNames(), Is.EquivalentTo(expected.Keys));

            Assert.That(actual.GetValues().Count, Is.EqualTo(expected.Values.Count));
            Assert.That(actual.GetValues(), Is.EquivalentTo(expected.Values));

            foreach ((string key, object value) in expected)
            {
                Assert.That(actual.Exists(key));
                Assert.That(actual[key], Is.EqualTo(value));

                object retrieved = actual.GetOptionalValue(key);
                Assert.That(retrieved, Is.EqualTo(value));

                Assert.That(actual.GetType(key), Is.EqualTo(value?.GetType() ?? typeof(object)));
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Converts a single node that represents relevant network information to one or more features.
        /// </summary>
        /// <param name="node">A node.</param>
        /// <param name="extraTags">Merge in extra tags.</param>
        /// <returns>A feature collection representing the node info.</returns>
        public static FeatureCollection ToFeatureCollection(this Node node, IEnumerable <Tag>?extraTags = null)
        {
            var features = new FeatureCollection();

            if (!node.Latitude.HasValue || !node.Longitude.HasValue)
            {
                return(features);
            }

            var attributes = new AttributesTable();

            if (extraTags != null)
            {
                foreach (var t in extraTags)
                {
                    if (attributes.Exists(t.Key))
                    {
                        attributes[t.Key] = t.Value;
                    }
                    else
                    {
                        attributes.Add(t.Key, t.Value);
                    }
                }
            }

            if (node.Tags != null)
            {
                foreach (var t in node.Tags)
                {
                    if (attributes.Exists(t.Key))
                    {
                        attributes[t.Key] = t.Value;
                    }
                    else
                    {
                        attributes.Add(t.Key, t.Value);
                    }
                }
            }

            features.Add(new Feature(new Point(new Coordinate(node.Longitude.Value, node.Latitude.Value)), attributes));

            return(features);
        }
        public static AttributesTable ToAttributesTable(this TagsCollectionBase tags)
        {
            var table = new AttributesTable();

            foreach (var t in tags)
            {
                if (table.Exists(t.Key))
                {
                    table[t.Key] = t.Value;
                }
                else
                {
                    table.Add(t.Key, t.Value);
                }
            }

            return(table);
        }
Esempio n. 6
0
        private static object InternalReadJson(JsonReader reader, JsonSerializer serializer,
                                               bool innerObject)
        {
            //// TODO: refactor to remove check when reading TopoJSON
            //if (reader.TokenType == JsonToken.StartArray)
            //{
            //    reader.Read(); // move to first item
            //    IList<object> array = new List<object>();
            //    do
            //    {
            //        if (reader.TokenType == JsonToken.EndArray) break;
            //        object inner = InternalReadJson(reader, serializer);
            //        array.Add(inner);
            //        reader.Read(); // move to next item
            //    } while (true);
            //    return array;
            //}

            if (reader.TokenType != JsonToken.StartObject)
            {
                throw new ArgumentException("Expected token '{' not found.");
            }

            // Advance reader
            reader.Read();
            reader.SkipComments();
            var attributesTable = new AttributesTable();

            if (reader.TokenType != JsonToken.Null)
            {
                while (reader.TokenType == JsonToken.PropertyName)
                {
                    string attributeName = (string)reader.Value;
                    reader.Read();
                    object attributeValue;
                    if (reader.TokenType == JsonToken.StartObject)
                    {
                        if (serializer.TypeNameHandling != TypeNameHandling.Objects)
                        {
                            // inner object to AttributeTable
                            attributeValue = InternalReadJson(reader, serializer, true);
                            if (reader.TokenType != JsonToken.EndObject)
                            {
                                throw new ArgumentException("Expected token '}' not found.");
                            }

                            // read EndObject token
                            reader.Read();
                        }
                        else
                        {
                            // deserialize the inner object
                            attributeValue = serializer.Deserialize(reader);
                        }
                    }
                    else if (reader.TokenType == JsonToken.StartArray)
                    {
                        attributeValue = InternalReadJsonArray(reader, serializer);
                        //reader.Read(); // move to first item
                        //IList<object> array = new List<object>();
                        //do
                        //{
                        //    object inner = InternalReadJson(reader, serializer);
                        //    array.Add(inner);
                        //    reader.Read(); // move to next item
                        //} while (reader.TokenType != JsonToken.EndArray);
                        //attributeValue = array;
                    }
                    else
                    {
                        attributeValue = reader.Value;
                        reader.Read();
                    }

                    if (!attributesTable.Exists(attributeName))
                    {
                        attributesTable.Add(attributeName, attributeValue);
                    }
                }

                reader.SkipComments();
            }

            // TODO: refactor to remove check when reading TopoJSON
            if (reader.TokenType != JsonToken.EndObject)
            {
                throw new ArgumentException("Expected token '}' not found.");
            }

            return(attributesTable);
        }
Esempio n. 7
0
        /// <summary>
        /// Converts a single route relation to one or more features.
        /// </summary>
        /// <param name="routeRelation">The route relation.</param>
        /// <returns>A feature collection representing the route relation.</returns>
        public static FeatureCollection ToFeatureCollection(this CompleteRelation routeRelation)
        {
            var features = new FeatureCollection();

            if (routeRelation.Members == null)
            {
                return(features);
            }

            foreach (var member in routeRelation.Members)
            {
                if (member.Member is not CompleteWay way)
                {
                    continue;
                }
                if (way.Nodes == null || way.Nodes.Length < 2)
                {
                    continue;
                }

                var lineString = way.ToLineString();

                var attributes = new AttributesTable();
                if (way?.Tags != null)
                {
                    foreach (var t in way.Tags)
                    {
                        if (attributes.Exists(t.Key))
                        {
                            attributes[t.Key] = t.Value;
                        }
                        else
                        {
                            attributes.Add(t.Key, t.Value);
                        }
                    }
                }

                if (routeRelation?.Tags != null)
                {
                    foreach (var t in routeRelation.Tags)
                    {
                        if (attributes.Exists(t.Key))
                        {
                            attributes[t.Key] = t.Value;
                        }
                        else
                        {
                            attributes.Add(t.Key, t.Value);
                        }
                    }
                }

                var key = "id";
                if (attributes.Exists(key))
                {
                    attributes[key] = member.Member.Id;
                }
                else
                {
                    attributes.Add(key, member.Member.Id);
                }

                key = "relation_id";
                if (attributes.Exists(key))
                {
                    attributes[key] = routeRelation.Id;
                }
                else
                {
                    attributes.Add(key, routeRelation.Id);
                }

                features.Add(new Feature(lineString, attributes));
            }

            return(features);
        }