Ejemplo n.º 1
0
        private static TopoObject[] ReadGeometries(JsonReader reader, JsonSerializer serializer)
        {
            reader.Read();
            if (reader.TokenType != JsonToken.StartArray)
            {
                throw new ArgumentException("Expected StartArray but was " + reader.TokenType);
            }
            reader.Read();
            List <TopoObject> list = new List <TopoObject>();

            while (reader.TokenType != JsonToken.EndArray)
            {
                TopoObject item = serializer.Deserialize <TopoObject>(reader);
                list.Add(item);
            }
            reader.Read();
            return(list.ToArray());
        }
        private FeatureCollection CreateCollection(TopoObject[] data)
        {
            IFeature[] features = new IFeature[data.Length];
            for (int i = 0; i < data.Length; i++)
            {
                TopoObject        component = data[i];
                FeatureCollection coll      = Create(component);
                features[i] = coll[0];
            }

            FeatureCollection collection = new FeatureCollection();

            foreach (IFeature feature in features)
            {
                collection.Add(feature);
            }
            return(collection);
        }
Ejemplo n.º 3
0
        void RenderTopoObject(TopoJson topo, TopoObject obj)
        {
            int geometryIndex = 0;

            foreach (var geometry in obj.geometries)
            {
                geometryIndex++;
                string name = null;

                if (geometry.properties != null)
                {
                    name = (string)geometry.properties.GetOrNull("nam");
                }
                if (name == null)
                {
                    name = geometryIndex.ToString();
                }

                if (geometry.IsMultiPolygon())
                {
                    var parentObject = this.NewGameObject(this.transform, name);

                    int[][] arcIndexesSet = geometry.AsMultiPolygon().arcs.Flatten().ToArray();

                    foreach (int[] arcIndexes in arcIndexesSet)
                    {
                        var verts = TopoPolygon.CreateVerts(arcIndexes, topo);
                        this.RenderGameObject(parentObject.transform, name, verts);
                    }
                }
                else if (geometry.IsPolygon())
                {
                    var parentObject = this.gameObject.transform;

                    int[] arcIndexes = geometry.AsPolygon().arcs.Flatten().ToArray();
                    var   verts      = TopoPolygon.CreateVerts(arcIndexes, topo);
                    this.RenderGameObject(parentObject.transform, name, verts);
                }
            }
        }
Ejemplo n.º 4
0
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            if (reader.TokenType != JsonToken.StartObject)
            {
                throw new ArgumentException("Expected StartObject but was " + reader.TokenType);
            }

            IDictionary <string, TopoObject> dict = new Dictionary <string, TopoObject>();

            do
            {
                reader.Read();
                if (reader.TokenType != JsonToken.PropertyName)
                {
                    throw new ArgumentException("Expected PropertyName but was " + reader.TokenType);
                }
                string key = (string)reader.Value;
                reader.Read();
                TopoObject val = serializer.Deserialize <TopoObject>(reader);
                dict.Add(key, val);
            }while (reader.TokenType != JsonToken.EndObject);
            return(dict);
        }
        public FeatureCollection Create(TopoObject data)
        {
            if (data == null)
            {
                throw new ArgumentNullException("data");
            }
            string type = data.Type;

            if (String.IsNullOrEmpty(type))
            {
                throw new ArgumentException("type undefined", "data");
            }

            if (String.Equals("GeometryCollection", type))
            {
                // a TopoJSON "GeometryCollection" actually is an IFeature array
                // so we handle this stuff as a special case
                TopoCollection coll = (TopoCollection)data;
                return(CreateCollection(coll.Geometries));
            }

            IGeometry geometry;

            switch (type)
            {
            case "Point":
                TopoPoint point = (TopoPoint)data;
                geometry = CreatePoint(point.Coordinates);
                break;

            case "MultiPoint":
                TopoMultiPoint mpoint = (TopoMultiPoint)data;
                geometry = CreateMultiPoint(mpoint.Coordinates);
                break;

            case "LineString":
                TopoLineString lstring = (TopoLineString)data;
                geometry = CreateLineString(lstring.Arcs);
                break;

            case "MultiLineString":
                TopoMultiLineString mlstring = (TopoMultiLineString)data;
                geometry = CreateMultiLineString(mlstring.Arcs);
                break;

            case "Polygon":
                TopoPolygon poly = (TopoPolygon)data;
                geometry = CreatePolygon(poly.Arcs);
                break;

            case "MultiPolygon":
                TopoMultiPolygon mpoly = (TopoMultiPolygon)data;
                geometry = CreateMultiPolygon(mpoly.Arcs);
                break;

            default:
                string s = string.Format("type unsupported: {0}", type);
                throw new NotSupportedException(s);
            }

            IAttributesTable      properties = data.Properties;
            Feature               feature    = new Feature(geometry, properties);
            Collection <IFeature> collection = new Collection <IFeature> {
                feature
            };

            return(new FeatureCollection(collection));
        }