Beispiel #1
0
        protected override async Task _draw()
        {
            RecordSet layer = GetMetadata();

            if (layer.Properties.BBox != null)
            {
                features.SetSpatialFilterRect(layer.Properties.BBox[0], layer.Properties.BBox[1], layer.Properties.BBox[2], layer.Properties.BBox[3]);
            }
            SetCrs(OgrReader.getSR(features, layer));
            using (OgrReader ogrReader = new OgrReader()) {
                await ogrReader.GetFeaturesAsync(features);

                foreach (Feature feature in ogrReader.features)
                {
                    int geoCount = feature.GetDefnRef().GetGeomFieldCount();
                    for (int j = 0; j < geoCount; j++)
                    {
                        Geometry        point = feature.GetGeomFieldRef(j);
                        wkbGeometryType type  = point.GetGeometryType();
                        string          t     = type.ToString();
                        if (point.GetGeometryType() == wkbGeometryType.wkbPoint ||
                            point.GetGeometryType() == wkbGeometryType.wkbPoint25D ||
                            point.GetGeometryType() == wkbGeometryType.wkbPointM ||
                            point.GetGeometryType() == wkbGeometryType.wkbPointZM)
                        {
                            point.TransformWorld(GetCrs()).ToList <Vector3>().ForEach(async item => await _drawFeatureAsync(item, feature));
                        }
                        else if
                        (point.GetGeometryType() == wkbGeometryType.wkbMultiPoint ||
                         point.GetGeometryType() == wkbGeometryType.wkbMultiPoint25D ||
                         point.GetGeometryType() == wkbGeometryType.wkbMultiPointM ||
                         point.GetGeometryType() == wkbGeometryType.wkbMultiPointZM)
                        {
                            int n = point.GetGeometryCount();
                            for (int k = 0; k < n; k++)
                            {
                                Geometry Point2 = point.GetGeometryRef(k);
                                Point2.TransformWorld(GetCrs()).ToList <Vector3>().ForEach(async item => await _drawFeatureAsync(item, feature));
                            }
                        }
                        point.Dispose();
                    }
                }
            }
            if (layer.Transform != null)
            {
                transform.position   = AppState.instance.map.transform.TransformPoint(layer.Transform.Position);
                transform.rotation   = layer.Transform.Rotate;
                transform.localScale = layer.Transform.Scale;
            }
        }
Beispiel #2
0
        protected override async Task _init()
        {
            RecordSet layer = _layer as RecordSet;
            string    ex    = Path.GetExtension(layer.Source).ToLower();

            if (ex != ".dxf")
            {
                DMesh3Builder meshes = await loadObj(layer.Source);

                features = meshes.Meshes;
                foreach (DMesh3 mesh in features)
                {
                    foreach (int idx in mesh.VertexIndices())
                    {
                        Vector3d vtx = mesh.GetVertex(idx);
                        mesh.SetVertex(idx, new Vector3d(vtx.x, vtx.z, vtx.y));
                    }
                }
                symbology = layer.Properties.Units;
            }
            if (ex == ".dxf")
            {
                List <Vector3d> vertexes = new List <Vector3d>();
                List <Index3i>  tris     = new List <Index3i>();

                try {
                    //
                    // Try opening with netDxf - this will only open files in autoCAD version 2000 or later
                    //
                    if (layer.Crs != null && layer.Crs != "")
                    {
                        SetCrs(Convert.TextToSR(layer.Crs));
                    }
                    DXF.DxfDocument doc;
                    using (Stream stream = File.Open(layer.Source, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) {
                        doc = DXF.DxfDocument.Load(stream);
                        stream.Close();
                    }
                    string layout = doc.ActiveLayout;
                    IEnumerable <Face3d>     faces     = doc.Faces3d;
                    List <DCurve3>           curves    = new List <DCurve3>();
                    CoordinateTransformation transform = new CoordinateTransformation(GetCrs(), AppState.instance.mapProj);
                    foreach (Face3d face in faces)
                    {
                        List <Vector3d> tri = new List <Vector3d>();
                        tri.Add(face.FirstVertex.ToVector3d(transform));
                        tri.Add(face.SecondVertex.ToVector3d(transform));
                        tri.Add(face.ThirdVertex.ToVector3d(transform));
                        if (face.FourthVertex != face.ThirdVertex)
                        {
                            Debug.Log(" Not a Tringle");
                        }
                        curves.Add(new DCurve3(tri, false, true));
                    }
                    //
                    // for each face, check to make sure that vertices are in the vertex list and add the tri to the tri list
                    //
                    foreach (DCurve3 curve in curves)
                    {
                        List <int> tri = new List <int>();
                        for (int i = 0; i < 3; i++)
                        {
                            Vector3d v     = curve.GetVertex(i);
                            int      index = vertexes.IndexOf(v);
                            if (index == -1)
                            {
                                vertexes.Add(v);
                                index = vertexes.IndexOf(v);
                            }
                            tri.Add(index);
                        }
                        tris.Add(new Index3i(tri.ToArray()));
                    }
                } catch {
                    //
                    // if netDXF fails - try opening in GDAL that can open AutoCAD 2 file
                    //
                    using (OgrReader ogrReader = new OgrReader()) {
                        await ogrReader.Load(layer.Source, layer.Properties.ReadOnly? 0 : 1, layer.Properties.SourceType);

                        entities = ogrReader.GetLayers()[0];
                        SetCrs(OgrReader.getSR(entities, layer));
                        RecordSet metadata = GetMetadata();
                        if (metadata.Properties.BBox != null)
                        {
                            entities.SetSpatialFilterRect(metadata.Properties.BBox[0], metadata.Properties.BBox[1], metadata.Properties.BBox[2], metadata.Properties.BBox[3]);
                        }
                        await ogrReader.GetFeaturesAsync(entities);

                        foreach (Feature feature in ogrReader.features)
                        {
                            Geometry geom = feature.GetGeometryRef();
                            if (geom == null)
                            {
                                continue;
                            }
                            wkbGeometryType ftype = geom.GetGeometryType();
                            OgrReader.Flatten(ref ftype);
                            //
                            // Get the faces
                            //
                            if (ftype == wkbGeometryType.wkbPolygon)
                            {
                                List <Geometry> LinearRings = new List <Geometry>();
                                List <DCurve3>  curves      = new List <DCurve3>();
                                for (int i = 0; i < geom.GetGeometryCount(); i++)
                                {
                                    LinearRings.Add(geom.GetGeometryRef(i));
                                }
                                //
                                // Load the faces as a list of DCurve3
                                //
                                foreach (Geometry LinearRing in LinearRings)
                                {
                                    wkbGeometryType type = LinearRing.GetGeometryType();
                                    if (type == wkbGeometryType.wkbLinearRing || type == wkbGeometryType.wkbLineString25D || type == wkbGeometryType.wkbLineString)
                                    {
                                        LinearRing.CloseRings();
                                        DCurve3 curve = new DCurve3();
                                        curve.FromGeometry(LinearRing, GetCrs());
                                        if (curve.VertexCount != 4)
                                        {
                                            Debug.LogError("incorrect face size");
                                        }
                                        else
                                        {
                                            curves.Add(curve);
                                        }
                                    }
                                }
                                //
                                // for each tri, check to make sure that vertcie are in the vertex list and add the tri to the tri list
                                //
                                foreach (DCurve3 curve in curves)
                                {
                                    List <int> tri = new List <int>();
                                    for (int i = 0; i < 3; i++)
                                    {
                                        Vector3d v     = curve.GetVertex(i);
                                        int      index = vertexes.IndexOf(v);
                                        if (index == -1)
                                        {
                                            vertexes.Add(v);
                                            index = vertexes.IndexOf(v);
                                        }
                                        tri.Add(index);
                                    }
                                    tris.Add(new Index3i(tri.ToArray()));
                                }
                            }
                        }
                    }
                }
                //
                // vertexes and tris should now describe a mesh
                //
                DMesh3 dmesh = new DMesh3(false, false, false, false);
                vertexes.ForEach(v => dmesh.AppendVertex(v));
                tris.ForEach(t => dmesh.AppendTriangle(t));
                features = new List <DMesh3>();
                features.Add(dmesh.Compactify());
                symbology = layer.Properties.Units;
                return;
            }
        }
Beispiel #3
0
        protected override async Task _init()
        {
            //
            // Load Dataset
            //
            RecordSet layer = _layer as RecordSet;

            ogrReader = new OgrReader();
            await ogrReader.Load(layer.Source, layer.Properties.ReadOnly? 0 : 1, layer.Properties.SourceType);

            //
            // Get and process features
            //
            features = ogrReader.GetLayers().ToArray();
            foreach (Layer thisLayer in features)
            {
                wkbGeometryType type = thisLayer.GetGeomType();
                OgrReader.Flatten(ref type);
                switch (type)
                {
                case wkbGeometryType.wkbPoint:
                    subLayers.Add(Instantiate(PointLayer, transform).GetComponent <PointLayer>());
                    (subLayers.Last() as VirgisLayer <RecordSet, Layer>).SetFeatures(thisLayer);
                    subLayers.Last().SetCrs(OgrReader.getSR(thisLayer, layer));
                    subLayers.Last().SetMetadata(layer);
                    subLayers.Last().sourceName = thisLayer.GetName();
                    await subLayers.Last().SubInit(layer);

                    break;

                case wkbGeometryType.wkbLineString:
                    subLayers.Add(Instantiate(LineLayer, transform).GetComponent <LineLayer>());
                    (subLayers.Last() as VirgisLayer <RecordSet, Layer>).SetFeatures(thisLayer);
                    subLayers.Last().SetCrs(OgrReader.getSR(thisLayer, layer));
                    subLayers.Last().SetMetadata(layer);
                    subLayers.Last().sourceName = thisLayer.GetName();
                    await subLayers.Last().SubInit(layer);

                    break;

                case wkbGeometryType.wkbPolygon:
                    subLayers.Add(Instantiate(PolygonLayer, transform).GetComponent <PolygonLayer>());
                    (subLayers.Last() as VirgisLayer <RecordSet, Layer>).SetFeatures(thisLayer);
                    subLayers.Last().SetCrs(OgrReader.getSR(thisLayer, layer));
                    subLayers.Last().SetMetadata(layer);
                    subLayers.Last().sourceName = thisLayer.GetName();
                    await subLayers.Last().SubInit(layer);

                    break;

                case wkbGeometryType.wkbTIN:
                    subLayers.Add(Instantiate(TinLayer, transform).GetComponent <TinLayer>());
                    (subLayers.Last() as VirgisLayer <RecordSet, Layer>).SetFeatures(thisLayer);
                    subLayers.Last().SetCrs(OgrReader.getSR(thisLayer, layer));
                    subLayers.Last().SetMetadata(layer);
                    subLayers.Last().sourceName = thisLayer.GetName();
                    await subLayers.Last().SubInit(layer);

                    break;

                //
                // If feature type is unknown, process each feature seperately
                //
                case wkbGeometryType.wkbUnknown:
                    RecordSet metadata = GetMetadata();
                    if (metadata.Properties.BBox != null)
                    {
                        thisLayer.SetSpatialFilterRect(metadata.Properties.BBox[0], metadata.Properties.BBox[1], metadata.Properties.BBox[2], metadata.Properties.BBox[3]);
                    }
                    await ogrReader.GetFeaturesAsync(thisLayer);

                    foreach (Feature feature in ogrReader.features)
                    {
                        if (feature == null)
                        {
                            continue;
                        }
                        Geometry geom = feature.GetGeometryRef();
                        if (geom == null)
                        {
                            continue;
                        }
                        wkbGeometryType ftype = geom.GetGeometryType();
                        OgrReader.Flatten(ref ftype);
                        VirgisLayer <RecordSet, Layer> layerToAdd = null;
                        switch (ftype)
                        {
                        case wkbGeometryType.wkbLineString:
                            foreach (VirgisLayer <RecordSet, Layer> l in subLayers)
                            {
                                if (l.GetType() == typeof(LineLayer))
                                {
                                    layerToAdd = l;
                                    break;
                                }
                            }
                            if (layerToAdd == null)
                            {
                                subLayers.Add(Instantiate(LineLayer, transform).GetComponent <LineLayer>());
                                subLayers.Last().SetCrs(OgrReader.getSR(thisLayer, layer));
                                (subLayers.Last() as VirgisLayer <RecordSet, Layer>).SetFeatures(thisLayer);
                                subLayers.Last().SetMetadata(layer);
                                await subLayers.Last().SubInit(layer);
                            }
                            break;

                        case wkbGeometryType.wkbPolygon:
                            foreach (VirgisLayer <RecordSet, Layer> l in subLayers)
                            {
                                if (l.GetType() == typeof(PolygonLayer))
                                {
                                    layerToAdd = l;
                                    break;
                                }
                            }
                            if (layerToAdd == null)
                            {
                                subLayers.Add(Instantiate(PolygonLayer, transform).GetComponent <PolygonLayer>());
                                subLayers.Last().SetCrs(OgrReader.getSR(thisLayer, layer));
                                (subLayers.Last() as VirgisLayer <RecordSet, Layer>).SetFeatures(thisLayer);
                                subLayers.Last().SetMetadata(layer);
                                await subLayers.Last().SubInit(layer);
                            }
                            break;

                        case wkbGeometryType.wkbPoint:
                            foreach (VirgisLayer <RecordSet, Layer> l in subLayers)
                            {
                                if (l.GetType() == typeof(PointLayer))
                                {
                                    layerToAdd = l;
                                    break;
                                }
                            }
                            if (layerToAdd == null)
                            {
                                subLayers.Add(Instantiate(PointLayer, transform).GetComponent <PointLayer>());
                                subLayers.Last().SetCrs(OgrReader.getSR(thisLayer, layer));
                                (subLayers.Last() as VirgisLayer <RecordSet, Layer>).SetFeatures(thisLayer);
                                subLayers.Last().SetMetadata(layer);
                                await subLayers.Last().SubInit(layer);
                            }
                            break;

                        case wkbGeometryType.wkbTIN:
                            foreach (VirgisLayer <RecordSet, Layer> l in subLayers)
                            {
                                if (l.GetType() == typeof(TinLayer))
                                {
                                    layerToAdd = l;
                                    break;
                                }
                            }
                            if (layerToAdd == null)
                            {
                                subLayers.Add(Instantiate(TinLayer, transform).GetComponent <TinLayer>());
                                subLayers.Last().SetCrs(OgrReader.getSR(thisLayer, layer));
                                (subLayers.Last() as VirgisLayer <RecordSet, Layer>).SetFeatures(thisLayer);
                                subLayers.Last().SetMetadata(layer);
                                await subLayers.Last().SubInit(layer);
                            }
                            geom.Dispose();
                            break;
                        }
                    }
                    return;
                }
            }
        }