コード例 #1
0
ファイル: PreviewControl.cs プロジェクト: jugstalt/gview5
        async private Task InvokeSetExplorerObject()
        {
            _exObjectInvokeRequired = false;

            mapView1.CancelDrawing(DrawPhase.All);
            foreach (IDatasetElement element in _map.MapElements)
            {
                _map.RemoveLayer(element as ILayer);
            }

            if (_exObject != null)
            {
                var instance = await _exObject.GetInstanceAsync();

                if (instance is IFeatureClass && ((IFeatureClass)instance).Envelope != null)
                {
                    mapView1.Map = _map;
                    _map.AddLayer(LayerFactory.Create(instance as IClass));
                    _map.Display.Limit = ((IFeatureClass)instance).Envelope;
                    _map.Display.ZoomTo(((IFeatureClass)instance).Envelope);
                }
                else if (instance is IRasterClass && ((IRasterClass)instance).Polygon != null)
                {
                    mapView1.Map = _map;
                    _map.AddLayer(LayerFactory.Create(instance as IClass));
                    _map.Display.Limit = ((IRasterClass)instance).Polygon.Envelope;
                    _map.Display.ZoomTo(((IRasterClass)instance).Polygon.Envelope);
                }
                else if (instance is IWebServiceClass && ((IWebServiceClass)instance).Envelope != null)
                {
                    mapView1.Map = _map;
                    _map.AddLayer(LayerFactory.Create(instance as IClass));
                    _map.Display.Limit = ((IWebServiceClass)instance).Envelope;
                    _map.Display.ZoomTo(((IWebServiceClass)instance).Envelope);
                }
                else if (instance is IFeatureDataset)
                {
                    mapView1.Map = _map;
                    IFeatureDataset dataset = (IFeatureDataset)instance;
                    foreach (IDatasetElement element in await dataset.Elements())
                    {
                        ILayer layer = LayerFactory.Create(element.Class) as ILayer;
                        if (layer == null)
                        {
                            continue;
                        }

                        _map.AddLayer(layer);
                    }
                    _map.Display.Limit = await dataset.Envelope();

                    _map.Display.ZoomTo(await dataset.Envelope());
                }
                else if (instance is Map)
                {
                    Map map = (Map)instance;

                    map.NewBitmap        -= InvokeNewBitmapCreated;
                    map.DoRefreshMapView -= InvokeDoRefreshMapView;

                    map.NewBitmap        += InvokeNewBitmapCreated;
                    map.DoRefreshMapView += InvokeDoRefreshMapView;

                    mapView1.Map = (Map)instance;
                }
            }
        }
コード例 #2
0
ファイル: FormAddDataset.cs プロジェクト: jugstalt/gview5
        async private void ButtonDatasetOK_Click(object sender, System.EventArgs e)
        {
            _datasets.Reset();
            IDataset dataset;

            bool   first = true;
            double minx = 0, miny = 0, maxx = 0, maxy = 0;

            bool firstDataset = (_map[0] == null);

            while ((dataset = _datasets.Next) != null)
            {
                FormDatasetProperties datasetProps = await FormDatasetProperties.CreateAsync(_map, dataset);

                if (datasetProps.ShowDialog() != DialogResult.OK)
                {
                    continue;
                }

                _map.AddDataset(dataset, 0);
                if (dataset is IFeatureDataset && _map is Map && _map is IDisplay)
                {
                    IFeatureDataset fDS      = (IFeatureDataset)dataset;
                    IEnvelope       mapLimit = ((IDisplay)_map).Limit;

                    var fDsEnvelope = await fDS.Envelope();

                    mapLimit.minx          = Math.Min(mapLimit.minx, fDsEnvelope.minx);
                    mapLimit.miny          = Math.Min(mapLimit.miny, fDsEnvelope.miny);
                    mapLimit.maxx          = Math.Max(mapLimit.maxx, fDsEnvelope.maxx);
                    mapLimit.maxy          = Math.Max(mapLimit.maxy, fDsEnvelope.maxy);
                    ((IDisplay)_map).Limit = mapLimit;

                    if (first)
                    {
                        minx  = fDsEnvelope.minx;
                        miny  = fDsEnvelope.miny;
                        maxx  = fDsEnvelope.maxx;
                        maxy  = fDsEnvelope.maxy;
                        first = false;
                    }
                    else
                    {
                        minx = Math.Min(fDsEnvelope.minx, minx);
                        miny = Math.Min(fDsEnvelope.miny, miny);
                        maxx = Math.Max(fDsEnvelope.maxx, maxx);
                        maxy = Math.Max(fDsEnvelope.maxy, maxy);
                    }
                }
                else if (dataset is IRasterDataset && _map is Map && _map is IDisplay)
                {
                    IRasterDataset fDS      = (IRasterDataset)dataset;
                    IEnvelope      mapLimit = ((IDisplay)_map).Limit;

                    var fDsEnvelope = await fDS.Envelope();

                    if (fDsEnvelope == null)
                    {
                        continue;
                    }

                    mapLimit.minx          = Math.Min(mapLimit.minx, fDsEnvelope.minx);
                    mapLimit.miny          = Math.Min(mapLimit.miny, fDsEnvelope.miny);
                    mapLimit.maxx          = Math.Max(mapLimit.maxx, fDsEnvelope.maxx);
                    mapLimit.maxy          = Math.Max(mapLimit.maxy, fDsEnvelope.maxy);
                    ((IDisplay)_map).Limit = mapLimit;

                    if (first)
                    {
                        minx  = fDsEnvelope.minx;
                        miny  = fDsEnvelope.miny;
                        maxx  = fDsEnvelope.maxx;
                        maxy  = fDsEnvelope.maxy;
                        first = false;
                    }
                    else
                    {
                        minx = Math.Min(fDsEnvelope.minx, minx);
                        miny = Math.Min(fDsEnvelope.miny, miny);
                        maxx = Math.Max(fDsEnvelope.maxx, maxx);
                        maxy = Math.Max(fDsEnvelope.maxy, maxy);
                    }
                }
            }
            if (!first)
            {
                if (_map is Map && firstDataset)
                {
                    ((Map)_map).ZoomTo(minx, miny, maxx, maxy);
                }
            }
        }
コード例 #3
0
        async public Task Run()
        {
            if (_dataset == null || !(_fdb is IFeatureDatabaseReplication) || _edgeFcs == null)
            {
                return;
            }

            IFeatureDatabaseReplication db = (IFeatureDatabaseReplication)_fdb;

            if (_dataset.Element(_networkName) != null)
            {
                MessageBox.Show("Featureclass '" + _networkName + "' already exists!");
                return;
            }
            bool       succeeded = false;
            List <int> FcIds     = new List <int>();

            try
            {
                ProgressReport report = new ProgressReport();

                int datasetId = await _fdb.DatasetID(_dataset.DatasetName);

                if (datasetId == -1)
                {
                    return;
                }

                NetworkBuilder networkBuilder = new NetworkBuilder(await _dataset.Envelope(), _tolerance);
                if (ReportProgress != null)
                {
                    networkBuilder.reportProgress += new ProgressReporterEvent(networkBuilder_reportProgress);
                }

                #region Spatial Index
                BinaryTreeDef edgeTreeDef = null, nodeTreeDef = null;
                foreach (IFeatureClass fc in _edgeFcs)
                {
                    BinaryTreeDef treeDef = await _fdb.BinaryTreeDef(fc.Name);

                    if (treeDef == null)
                    {
                        IEnvelope bounds = fc.Envelope;
                        if (Envelope.IsNull(bounds))
                        {
                            continue;
                        }
                        treeDef = new BinaryTreeDef(fc.Envelope, 10);
                    }
                    if (edgeTreeDef == null)
                    {
                        edgeTreeDef = new BinaryTreeDef(treeDef.Bounds, Math.Min(treeDef.MaxLevel, 10));
                    }
                    else
                    {
                        Envelope bounds = edgeTreeDef.Bounds;
                        bounds.Union(treeDef.Bounds);
                        edgeTreeDef = new BinaryTreeDef(bounds, Math.Min(Math.Max(edgeTreeDef.MaxLevel, treeDef.MaxLevel), 10));
                    }
                }
                foreach (IFeatureClass fc in _nodeFcs)
                {
                    BinaryTreeDef treeDef = await _fdb.BinaryTreeDef(fc.Name);

                    if (treeDef == null)
                    {
                        IEnvelope bounds = fc.Envelope;
                        if (Envelope.IsNull(bounds))
                        {
                            continue;
                        }
                        treeDef = new BinaryTreeDef(fc.Envelope, 10);
                    }
                    if (nodeTreeDef == null)
                    {
                        nodeTreeDef = new BinaryTreeDef(treeDef.Bounds, Math.Min(treeDef.MaxLevel, 10));
                    }
                    else
                    {
                        Envelope bounds = nodeTreeDef.Bounds;
                        bounds.Union(treeDef.Bounds);
                        nodeTreeDef = new BinaryTreeDef(bounds, Math.Min(Math.Max(nodeTreeDef.MaxLevel, treeDef.MaxLevel), 10));
                    }
                }
                #endregion

                #region Add Edges
                foreach (IFeatureClass fc in _edgeFcs)
                {
                    int fcId = await _fdb.FeatureClassID(datasetId, fc.Name);

                    if (fcId == -1)
                    {
                        continue;
                    }
                    FcIds.Add(fcId);

                    bool        useOneway = false;
                    QueryFilter filter    = new QueryFilter();
                    filter.AddField(fc.IDFieldName);
                    filter.AddField(fc.ShapeFieldName);
                    if (!String.IsNullOrEmpty(_onewayFieldName) && fc.FindField(_onewayFieldName) != null)
                    {
                        filter.AddField(_onewayFieldName);
                        useOneway = true;
                    }
                    Dictionary <Guid, IGraphWeightFeatureClass> gwfcs = new Dictionary <Guid, IGraphWeightFeatureClass>();
                    if (_graphWeights != null)
                    {
                        foreach (IGraphWeight weight in _graphWeights)
                        {
                            foreach (IGraphWeightFeatureClass gwfc in weight.FeatureClasses)
                            {
                                if (gwfc.FcId == fcId && !String.IsNullOrEmpty(gwfc.FieldName))
                                {
                                    gwfcs.Add(weight.Guid, gwfc);
                                    filter.AddField(gwfc.FieldName);
                                }
                            }
                        }
                    }
                    if (gwfcs.Keys.Count == 0)
                    {
                        gwfcs = null;
                    }

                    bool useWithComplexEdges = false;
                    if (_complexEdgeFcs != null &&
                        (_complexEdgeFcs.Contains(fcId) || _complexEdgeFcs.Contains(-1)))
                    {
                        useWithComplexEdges = true;
                    }

                    using (IFeatureCursor cursor = await fc.GetFeatures(filter))
                    {
                        #region Report
                        report.Message    = "Analize Edges: " + fc.Name;
                        report.featureMax = await fc.CountFeatures();

                        report.featurePos = 0;
                        if (ReportProgress != null)
                        {
                            ReportProgress(report);
                        }
                        #endregion

                        IFeature feature;
                        while ((feature = await cursor.NextFeature()) != null)
                        {
                            bool oneway = false;
                            if (useOneway)
                            {
                                int ow = Convert.ToInt32(feature[_onewayFieldName]);
                                oneway = (ow != 0);
                            }
                            Hashtable gw = null;
                            if (gwfcs != null)
                            {
                                foreach (Guid weightGuid in gwfcs.Keys)
                                {
                                    IGraphWeightFeatureClass gwfc = gwfcs[weightGuid];
                                    object objVal = feature[gwfc.FieldName];

                                    double val = (objVal == DBNull.Value) ? 0.0 : Convert.ToDouble(feature[gwfc.FieldName]);
                                    if (gwfc.SimpleNumberCalculation != null)
                                    {
                                        val = gwfc.SimpleNumberCalculation.Calculate(val);
                                    }
                                    if (gw == null)
                                    {
                                        gw = new Hashtable();
                                    }
                                    gw.Add(weightGuid, val);
                                }
                            }
                            networkBuilder.AddEdgeFeature(fcId, feature, oneway, gw, useWithComplexEdges);

                            #region Report
                            report.featurePos++;
                            if (report.featurePos % 1000 == 0)
                            {
                                if (ReportProgress != null)
                                {
                                    ReportProgress(report);
                                }
                            }
                            #endregion
                        }
                    }
                }
                #endregion

                #region Calculate ComplexEdges
                if (_complexEdgeFcs != null && _complexEdgeFcs.Count > 0)
                {
                    List <NetworkNode> networkNodes = networkBuilder.NetworkNodes.ToList();

                    #region Report
                    report.Message    = "Create Complex Edges...";
                    report.featureMax = networkNodes.Count;
                    report.featurePos = 0;
                    if (ReportProgress != null)
                    {
                        ReportProgress(report);
                    }
                    #endregion

                    foreach (NetworkNode node in networkNodes)
                    {
                        networkBuilder.SplitToComplexEdges(node);

                        #region Report
                        report.featurePos++;
                        if (report.featurePos % 1000 == 0)
                        {
                            if (ReportProgress != null)
                            {
                                ReportProgress(report);
                            }
                        }
                        #endregion
                    }
                }
                #endregion

                #region Add Nodes
                if (_nodeFcs != null)
                {
                    foreach (IFeatureClass fc in _nodeFcs)
                    {
                        int fcId = await _fdb.FeatureClassID(datasetId, fc.Name);

                        if (fcId == -1)
                        {
                            continue;
                        }
                        FcIds.Add(fcId);

                        bool   isSwitchable         = false;
                        string switchStateFieldname = String.Empty;
                        if (_switchNodeFcs != null && _switchNodeFcs.ContainsKey(fcId))
                        {
                            isSwitchable         = true;
                            switchStateFieldname = _switchNodeFcs[fcId];
                        }
                        NetworkNodeType nodeType = NetworkNodeType.Unknown;
                        if (_nodeTypeFcs != null && _nodeTypeFcs.ContainsKey(fcId))
                        {
                            nodeType = _nodeTypeFcs[fcId];
                        }

                        QueryFilter filter = new QueryFilter();
                        filter.AddField(fc.IDFieldName);
                        filter.AddField(fc.ShapeFieldName);
                        filter.AddField(switchStateFieldname);

                        using (IFeatureCursor cursor = await fc.GetFeatures(filter))
                        {
                            #region Report
                            report.Message    = "Analize Nodes: " + fc.Name;
                            report.featureMax = await fc.CountFeatures();

                            report.featurePos = 0;
                            if (ReportProgress != null)
                            {
                                ReportProgress(report);
                            }
                            #endregion

                            IFeature feature;
                            while ((feature = await cursor.NextFeature()) != null)
                            {
                                bool switchState = isSwitchable;
                                if (isSwitchable && !String.IsNullOrEmpty(switchStateFieldname))
                                {
                                    object so = feature[switchStateFieldname];
                                    if (so != null)
                                    {
                                        if (so is bool)
                                        {
                                            switchState = (bool)so;
                                        }
                                        else
                                        {
                                            try
                                            {
                                                switchState = Convert.ToInt32(so) > 0;
                                            }
                                            catch { switchState = false; }
                                        }
                                    }
                                }
                                networkBuilder.AddNodeFeature(fcId, feature, isSwitchable, switchState, nodeType);

                                #region Report
                                report.featurePos++;
                                if (report.featurePos % 1000 == 0)
                                {
                                    if (ReportProgress != null)
                                    {
                                        ReportProgress(report);
                                    }
                                }
                                #endregion
                            }
                        }
                    }
                }
                #endregion

                #region CreateGraph
                #region Report
                report.Message    = "Create Graph";
                report.featurePos = 0;
                if (ReportProgress != null)
                {
                    ReportProgress(report);
                }
                #endregion

                networkBuilder.CreateGraph();
                #endregion

                #region Create Edge Featureclass

                #region Simple Edges Table
                Fields fields = new Fields();
                fields.Add(new Field("Page", FieldType.integer));
                fields.Add(new Field("Data", FieldType.binary));
                await db.CreateIfNotExists(_networkName + "_Edges", fields);

                #endregion

                #region Edge Index Table
                fields = new Fields();
                fields.Add(new Field("EID", FieldType.integer));
                fields.Add(new Field("FCID", FieldType.integer));
                fields.Add(new Field("OID", FieldType.integer));
                fields.Add(new Field("ISCOMPLEX", FieldType.boolean));
                await db.CreateIfNotExists(_networkName + "_EdgeIndex", fields);

                #endregion

                #region Complex Edges FeatureClass
                fields = new Fields();
                fields.Add(new Field("EID", FieldType.integer));
                fields.Add(new Field("N1", FieldType.integer));
                fields.Add(new Field("N2", FieldType.integer));
                fields.Add(new Field("FCID", FieldType.integer));
                fields.Add(new Field("OID", FieldType.integer));
                //fields.Add(new Field("Length", FieldType.integer));
                //fields.Add(new Field("GeoLength", FieldType.integer));

                await _fdb.ReplaceFeatureClass(_dataset.DatasetName,
                                               _networkName + "_ComplexEdges",
                                               new GeometryDef(geometryType.Polyline),
                                               fields);

                IDatasetElement element = await _dataset.Element(_networkName + "_ComplexEdges");

                if (element == null || !(element.Class is IFeatureClass))
                {
                    return;
                }
                if (edgeTreeDef != null)
                {
                    await _fdb.SetSpatialIndexBounds(_networkName + "_ComplexEdges", "BinaryTree2", edgeTreeDef.Bounds, edgeTreeDef.SplitRatio, edgeTreeDef.MaxPerNode, edgeTreeDef.MaxLevel);
                }
                #endregion

                int             edge_page = 0;
                List <IFeature> features = new List <IFeature>(), features2 = new List <IFeature>();
                List <IRow>     rows   = new List <IRow>();
                IFeatureClass   edgeFc = (IFeatureClass)element.Class;
                IFeatureCursor  c      = networkBuilder.Edges;
                IFeature        f;

                #region Report
                report.Message = "Create Edges";
                if (c is ICount)
                {
                    report.featureMax = ((ICount)c).Count;
                }
                report.featurePos = 0;
                if (ReportProgress != null)
                {
                    ReportProgress(report);
                }
                #endregion

                string tabEdgesName     = _fdb.TableName(_networkName + "_Edges");
                string tabEdgeIndexName = _fdb.TableName(_networkName + "_EdgeIndex");

                while ((f = await c.NextFeature()) != null)
                {
                    int eid = (int)f["EID"];
                    if ((bool)f["ISCOMPLEX"] == true)
                    {
                        #region Complex Edges
                        features.Add(f);

                        report.featurePos++;
                        if (features.Count > 0 && features.Count % 1000 == 0)
                        {
                            if (ReportProgress != null)
                            {
                                ReportProgress(report);
                            }
                            await _fdb.Insert(edgeFc, features);

                            features.Clear();
                        }
                        #endregion
                    }
                    #region Edges Table
                    features2.Add(f);
                    if (NetworkObjectSerializer.Page(eid + 1) > edge_page)
                    {
                        IRow row = new Row();
                        row.Fields.Add(new FieldValue("Page", edge_page++));
                        row.Fields.Add(new FieldValue("Data", NetworkObjectSerializer.SerializeEdges(features2)));
                        db.InsertRow(tabEdgesName, row, null);
                        features2.Clear();
                    }
                    #endregion
                    #region Edge Index
                    IRow eir = new Row();
                    eir.Fields.Add(new FieldValue("EID", (int)f["EID"]));
                    eir.Fields.Add(new FieldValue("FCID", (int)f["FCID"]));
                    eir.Fields.Add(new FieldValue("OID", (int)f["OID"]));
                    eir.Fields.Add(new FieldValue("ISCOMPLEX", (bool)f["ISCOMPLEX"]));
                    rows.Add(eir);
                    if (rows.Count > 0 && rows.Count % 1000 == 0)
                    {
                        _fdb.InsertRows(tabEdgeIndexName, rows, null);
                        rows.Clear();
                    }

                    #endregion
                }
                if (rows.Count > 0)
                {
                    _fdb.InsertRows(tabEdgeIndexName, rows, null);
                    rows.Clear();
                }
                if (features2.Count > 0)
                {
                    IRow row = new Row();
                    row.Fields.Add(new FieldValue("Page", edge_page));
                    row.Fields.Add(new FieldValue("Data", NetworkObjectSerializer.SerializeEdges(features2)));
                    db.InsertRow(tabEdgesName, row, null);
                    features2.Clear();
                }
                if (features.Count > 0)
                {
                    if (ReportProgress != null)
                    {
                        ReportProgress(report);
                    }
                    await _fdb.Insert(edgeFc, features);
                }
                await _fdb.CalculateExtent(edgeFc);

                #endregion
                #region Create Weights
                if (_graphWeights != null)
                {
                    foreach (IGraphWeight weight in _graphWeights)
                    {
                        fields = new Fields();
                        fields.Add(new Field("Page", FieldType.integer));
                        fields.Add(new Field("Data", FieldType.binary));
                        await db.CreateIfNotExists(_networkName + "_Weights_" + weight.Guid.ToString("N").ToLower(), fields);

                        string tabWeightName = _fdb.TableName(_networkName + "_Weights_" + weight.Guid.ToString("N").ToLower());

                        NetworkEdges edges       = networkBuilder.NetworkEdges;
                        int          weight_page = 0;
                        int          counter     = 0;
                        BinaryWriter bw          = NetworkObjectSerializer.GetBinaryWriter();
                        foreach (NetworkEdge edge in edges)
                        {
                            counter++;
                            if (NetworkObjectSerializer.Page(counter) > weight_page)
                            {
                                IRow row = new Row();
                                row.Fields.Add(new FieldValue("Page", weight_page++));
                                row.Fields.Add(new FieldValue("Data", NetworkObjectSerializer.GetBuffer(bw)));
                                db.InsertRow(tabWeightName, row, null);
                                bw = NetworkObjectSerializer.GetBinaryWriter();
                            }

                            if (edge.Weights != null && edge.Weights.ContainsKey(weight.Guid))
                            {
                                NetworkObjectSerializer.WriteWeight(bw, weight, (double)edge.Weights[weight.Guid]);
                            }
                            else
                            {
                                NetworkObjectSerializer.WriteWeight(bw, weight, (double)0.0);
                            }
                        }

                        if (bw.BaseStream.Position > 0)
                        {
                            IRow row = new Row();
                            row.Fields.Add(new FieldValue("Page", weight_page++));
                            row.Fields.Add(new FieldValue("Data", NetworkObjectSerializer.GetBuffer(bw)));
                            db.InsertRow(tabWeightName, row, null);
                        }
                    }
                }
                #endregion
                #region Create Node Featureclass
                fields = new Fields();
                //fields.Add(new Field("NID", FieldType.integer));
                //fields.Add(new Field("G1", FieldType.integer));
                //fields.Add(new Field("G2", FieldType.integer));
                fields.Add(new Field("SWITCH", FieldType.boolean));
                fields.Add(new Field("STATE", FieldType.boolean));
                fields.Add(new Field("FCID", FieldType.integer));
                fields.Add(new Field("OID", FieldType.integer));
                fields.Add(new Field("NODETYPE", FieldType.integer));

                await _fdb.ReplaceFeatureClass(_dataset.DatasetName,
                                               _networkName + "_Nodes",
                                               new GeometryDef(geometryType.Point),
                                               fields);

                element = await _dataset.Element(_networkName + "_Nodes");

                if (element == null || !(element.Class is IFeatureClass))
                {
                    return;
                }
                if (nodeTreeDef != null)
                {
                    await _fdb.SetSpatialIndexBounds(_networkName + "_Nodes", "BinaryTree2", nodeTreeDef.Bounds, nodeTreeDef.SplitRatio, nodeTreeDef.MaxPerNode, nodeTreeDef.MaxLevel);
                }
                else if (edgeTreeDef != null)
                {
                    await _fdb.SetSpatialIndexBounds(_networkName + "_Nodes", "BinaryTree2", edgeTreeDef.Bounds, edgeTreeDef.SplitRatio, edgeTreeDef.MaxPerNode, edgeTreeDef.MaxLevel);
                }

                features.Clear();
                IFeatureClass nodeFc = (IFeatureClass)element.Class;
                c = networkBuilder.Nodes;

                #region Report
                report.Message = "Create Nodes";
                if (c is ICount)
                {
                    report.featureMax = ((ICount)c).Count;
                }
                report.featurePos = 0;
                if (ReportProgress != null)
                {
                    ReportProgress(report);
                }
                #endregion

                while ((f = await c.NextFeature()) != null)
                {
                    features.Add(f);

                    report.featurePos++;
                    if (features.Count > 0 && features.Count % 1000 == 0)
                    {
                        if (ReportProgress != null)
                        {
                            ReportProgress(report);
                        }
                        await _fdb.Insert(nodeFc, features);

                        features.Clear();
                    }
                }
                if (features.Count > 0)
                {
                    if (ReportProgress != null)
                    {
                        ReportProgress(report);
                    }
                    await _fdb.Insert(nodeFc, features);
                }
                await _fdb.CalculateExtent(nodeFc);

                #endregion
                #region Create Graph Class
                int graph_page = 0;
                fields = new Fields();
                fields.Add(new Field("Page", FieldType.integer));
                fields.Add(new Field("Data", FieldType.binary));
                //fields.Add(new Field("N1", FieldType.integer));
                //fields.Add(new Field("N2", FieldType.integer));
                //fields.Add(new Field("EID", FieldType.integer));
                //fields.Add(new Field("LENGTH", FieldType.Double));
                //fields.Add(new Field("GEOLENGTH", FieldType.Double));

                await _fdb.ReplaceFeatureClass(_dataset.DatasetName,
                                               _networkName,
                                               new GeometryDef(geometryType.Network),
                                               fields);

                element = await _dataset.Element(_networkName);

                if (element == null || !(element.Class is IFeatureClass))
                {
                    return;
                }

                features.Clear();
                IFeatureClass networkFc = (IFeatureClass)element.Class;
                c = networkBuilder.Graph;

                #region Report
                report.Message = "Create Network";
                if (c is ICount)
                {
                    report.featureMax = ((ICount)c).Count;
                }
                report.featurePos = 0;
                if (ReportProgress != null)
                {
                    ReportProgress(report);
                }
                #endregion

                string fcNetworkName = _fdb.TableName("FC_" + _networkName);

                while ((f = await c.NextFeature()) != null)
                {
                    report.featurePos++;

                    // Wenn aktuelles Feature in neue Page gehört ->
                    // bestehende, vor neuem Einfügen speichern und pageIndex erhöhen (graph_page++)
                    if (NetworkObjectSerializer.Page((int)f["N1"]) > graph_page)
                    {
                        if (ReportProgress != null)
                        {
                            ReportProgress(report);
                        }
                        IRow row = new Row();
                        row.Fields.Add(new FieldValue("Page", graph_page++));
                        row.Fields.Add(new FieldValue("Data", NetworkObjectSerializer.SerializeGraph(features)));
                        db.InsertRow(fcNetworkName, row, null);
                        features.Clear();
                    }

                    features.Add(f);
                }

                if (features.Count > 0)
                {
                    IRow row = new Row();
                    row.Fields.Add(new FieldValue("Page", graph_page));
                    row.Fields.Add(new FieldValue("Data", NetworkObjectSerializer.SerializeGraph(features)));
                    db.InsertRow(fcNetworkName, row, null);
                    features.Clear();
                }
                #endregion

                #region Create FDB_Networks
                int netId = await _fdb.GetFeatureClassID(_networkName);

                fields = new Fields();
                fields.Add(new Field("ID", FieldType.integer));
                fields.Add(new Field("Properties", FieldType.binary));
                await db.CreateIfNotExists("FDB_Networks", fields);

                NetworkObjectSerializer.NetworkProperties networkProps = new NetworkObjectSerializer.NetworkProperties(
                    NetworkObjectSerializer.PageSize, _tolerance);

                IRow row2 = new Row();
                row2.Fields.Add(new FieldValue("ID", netId));
                row2.Fields.Add(new FieldValue("Properties", networkProps.Serialize()));
                db.InsertRow(_fdb.TableName("FDB_Networks"), row2, null);
                #endregion

                #region Create FDB_NetworkClasses
                fields = new Fields();
                fields.Add(new Field("NetworkId", FieldType.integer));
                fields.Add(new Field("FCID", FieldType.integer));
                fields.Add(new Field("Properties", FieldType.binary));

                await db.CreateIfNotExists("FDB_NetworkClasses", fields);

                foreach (int fcId in FcIds)
                {
                    bool   isSwitchable         = (_switchNodeFcs != null && _switchNodeFcs.ContainsKey(fcId));
                    string switchStateFieldname = (isSwitchable ? _switchNodeFcs[fcId] : String.Empty);

                    NetworkObjectSerializer.NetworkClassProperties nwclsProps = new NetworkObjectSerializer.NetworkClassProperties(
                        _complexEdgeFcs.Contains(fcId),
                        isSwitchable, switchStateFieldname);

                    IRow row = new Row();
                    row.Fields.Add(new FieldValue("NetworkId", netId));
                    row.Fields.Add(new FieldValue("FCID", fcId));
                    row.Fields.Add(new FieldValue("Properties", nwclsProps.Serialize()));
                    db.InsertRow(_fdb.TableName("FDB_NetworkClasses"), row, null);
                }
                #endregion

                #region FDB_NetworkWeighs
                fields = new Fields();
                fields.Add(new Field("NetworkId", FieldType.integer));
                fields.Add(new Field("Name", FieldType.String));
                fields.Add(new Field("WeightGuid", FieldType.guid));
                fields.Add(new Field("Properties", FieldType.binary));

                await db.CreateIfNotExists("FDB_NetworkWeights", fields);

                if (_graphWeights != null)
                {
                    foreach (IGraphWeight weight in _graphWeights)
                    {
                        IRow row = new Row();
                        row.Fields.Add(new FieldValue("NetworkId", netId));
                        row.Fields.Add(new FieldValue("Name", weight.Name));
                        row.Fields.Add(new FieldValue("WeightGuid", weight.Guid));
                        row.Fields.Add(new FieldValue("Properties", NetworkObjectSerializer.SerializeWeight(weight)));
                        db.InsertRow(_fdb.TableName("FDB_NetworkWeights"), row, null);
                    }
                }
                #endregion

                succeeded = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                if (!succeeded)
                {
                    if (_fdb is IAlterDatabase)
                    {
                        ((IAlterDatabase)_fdb).DropTable(_networkName + "_Edges");
                    }
                    await _fdb.DeleteFeatureClass(_networkName + "_ComplexEdges");

                    await _fdb.DeleteFeatureClass(_networkName + "_Nodes");

                    await _fdb.DeleteFeatureClass(_networkName);
                }
            }
        }