Exemple #1
0
    /// <summary>
    /// Called when the "Import" button is clicked
    /// </summary>
    private void OnWizardCreate()
    {
        GameObject parentGameObject = new GameObject();

        parentGameObject.name = this.ParentName;

        try
        {
            FeatureCollection features = GeoJSONObject.Deserialize(this.GeoJSONSource.text);

            MapBounds bounds = features.bbox;
            bounds.ProjectToWebMercator();
            bounds.SetScale(this.HorizontalScale);

            this.UpdateCameraParameters(bounds.Center.ToVector3(), this.HorizontalScale, this.VerticalScale);

            foreach (FeatureObject ftr in features.features)
            {
                MapFeature feature = new MapFeature(ftr.properties[this.ObjectIDFieldName]);

                List <Vertex> vertices = ftr.geometry.AllPositions().ConvertAll((v) => new Vertex(v.latitude, v.longitude, 0.0));

                feature.SetAttributes(ftr.properties);
                feature.SetGeometry(EnumGeometryType.Polygon, vertices);

                if (feature.Geometry.IsEmpty)
                {
                    continue;
                }

                feature.Geometry.ProjectToWebMercator();
                feature.Geometry.SetScale(this.HorizontalScale);

                Vector3 cityOrigin = feature.Geometry.GetCentroid();

                GameObject go = feature.ToGameObject();
                go.transform.position = cityOrigin - bounds.Center.ToVector3();
                go.transform.parent   = parentGameObject.transform;

                Material material = new Material(Shader.Find("Standard"));
                material.color = Color.gray;// UnityEngine.Random.ColorHSV();
                go.GetComponent <Renderer>().material = material;

                if (this.Extrude)
                {
                    float height = feature.Attributes.Get <float>(this.ExtrudeField);

                    if (height > 0)
                    {
                        height = height * this.ExtrudeFactor;
                        MeshExtrusion.Extrude(go, height, this.InvertFaces);
                    }
                }
            }
        }
        catch (Exception ex)
        {
            Debug.Log(ex);
        }
    }
        /// <summary>
        /// Reads GML data
        /// </summary>
        /// <param name="rootElement">Root document element of the GML file</param>
        private void parseGMLDocument(string rootElementName, XmlElement rootElement)
        {
            try
            {
                #region READ FEATURES BOUNDS

                XmlNode bounds = rootElement.SelectSingleNode("/ogr:FeatureCollection/gml:boundedBy/gml:Envelope", GMLNamespaces);

                string lowerLeftCoordinates  = bounds.SelectSingleNode("gml:lowerCorner", GMLNamespaces).ChildNodes[0].Value;
                string upperRightCoordinates = bounds.SelectSingleNode("gml:upperCorner", GMLNamespaces).ChildNodes[0].Value;

                this.Bounds = new MapBounds(new Vertex(lowerLeftCoordinates), new Vertex(upperRightCoordinates));

                #endregion

                #region READ FEATURES

                this.Features = new List <MapFeature>();

                XmlNodeList featureNodes = rootElement.SelectNodes("/ogr:FeatureCollection/ogr:featureMember/ogr:" + rootElementName, GMLNamespaces);

                foreach (XmlNode featureNode in featureNodes)
                {
                    string fid = featureNode.SelectSingleNode("ogr:osm_id", GMLNamespaces).ChildNodes[0].Value;

                    int    dimension = this.GetAttribute <int>("srsDimension", featureNode.SelectSingleNode("ogr:geometryProperty/gml:Polygon/gml:exterior/gml:LinearRing/gml:posList", GMLNamespaces).Attributes);
                    string geometry  = featureNode.SelectSingleNode("ogr:geometryProperty/gml:Polygon/gml:exterior/gml:LinearRing/gml:posList", GMLNamespaces).ChildNodes[0].Value;

                    List <Vertex> verticesList = new List <Vertex>();
                    string[]      vertices     = geometry.Split(new char[] { ' ' });

                    switch (dimension)
                    {
                    // X Y Z
                    case 3:
                    {
                        for (int i = 0; i < vertices.Length - 3; i = i + 3)
                        {
                            verticesList.Add(new Vertex(String.Format("{0} {1} {2}", vertices[i], vertices[i + 1], vertices[i + 2])));
                        }

                        break;
                    }

                    // X Y
                    case 2:
                    {
                        for (int i = 0; i < vertices.Length - 2; i = i + 2)
                        {
                            verticesList.Add(new Vertex(String.Format("{0} {1}", vertices[i], vertices[i + 1])));
                        }

                        break;
                    }

                    default: break;
                    }

                    MapFeature feature = new MapFeature(fid);
                    feature.SetGeometry(EnumGeometryType.Polygon, verticesList);

                    this.Features.Add(feature);
                }

                #endregion
            }
            catch
            {
            }
        }