示例#1
0
        public void OpenTest()
        {
            SpatialLiteDB target = new SpatialLiteDB(@"C:\temp\2a3be691-72d2-4ffd-8fe1-ef2638ca531c.db");


            Assert.Inconclusive("A method that does not return a value cannot be verified.");
        }
示例#2
0
        /// <summary>
        /// Gets the Spatialite table.
        /// </summary>
        /// <returns>SpatialiteTable</returns>
        private SpatialiteTable GetSpatialTable(string databasePath, string tableName)
        {
            try
            {
                SpatialiteTable spatialiteTable = null;

                SpatialLiteDB db = new SpatialLiteDB(databasePath);

                db.Open();

                List <SpatialiteTable> tabNames = db.TableNames();

                foreach (SpatialiteTable table in tabNames)
                {
                    if (table.TableName.Equals(tableName))
                    {
                        spatialiteTable = table;
                    }
                }

                db.Close();

                return(spatialiteTable);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine(ex.StackTrace);
                throw;
            }
        }
示例#3
0
        public void CreateNewDatabaseTest()
        {
            string path = @"c:\temp\" + System.Guid.NewGuid().ToString() + ".db";

            SpatialLiteDB.CreateNewDatabase(path);


            Assert.IsTrue(System.IO.File.Exists(path));
        }
示例#4
0
        /// <summary>
        /// Creates the spatialite fields.
        /// </summary>
        /// <param name="fields">reference to IFieldsEdit</param>
        private void CreateSpatialiteFields(ref IFieldsEdit fields)
        {
            try
            {
                SpatialLiteDB db = new SpatialLiteDB(this.DatabasePath);
                db.Open();

                string sql = "PRAGMA table_info(\"" + this.TableName + "\")";

                using (SQLiteCommand command = new SQLiteCommand(sql, db.SQLiteDatabaseConn))
                {
                    using (SQLiteDataReader reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            string spatialiteFieldType = reader["type"].ToString();

                            if (!(spatialiteFieldType.Equals("LINESTRING") ||
                                  spatialiteFieldType.Equals("POINT") ||
                                  spatialiteFieldType.Equals("POLYGON") ||
                                  spatialiteFieldType.Equals("MULTIPOLYGON") ||
                                  string.IsNullOrEmpty(spatialiteFieldType)))
                            {
                                IField     nameField     = new FieldClass();
                                IFieldEdit nameFieldEdit = (IFieldEdit)nameField;
                                nameFieldEdit.Name_2 = reader["name"].ToString();
                                esriFieldType fieldType = GetFieldType(reader["type"].ToString());
                                nameFieldEdit.Type_2 = fieldType;

                                if (fieldType.Equals(esriFieldType.esriFieldTypeString))
                                {
                                    nameFieldEdit.Length_2 = MaxTextLength(db.SQLiteDatabaseConn, this.TableName, reader["name"].ToString());
                                }

                                fields.AddField(nameField);
                            }
                        }
                    }
                }
                db.Close();
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.StackTrace);
                throw;
            }
        }
示例#5
0
        private void buttonExport_Click(object sender, EventArgs e)
        {
            isExporting = true;
            this.StartExportingEvent(this, new EventArgs());

            try
            {
                string path = @"c:\temp\" + System.Guid.NewGuid().ToString() + ".spatialite";
                SpatialLiteDB.CreateNewDatabase(path);

                SpatialiteExporter exporter = new SpatialiteExporter(path);

                if (this.checkBoxExportCurrentExtent.Checked)
                {
                    exporter.Extent = this.MxDocument.ActiveView.Extent;
                }

                if (this.ExportLayers != null)
                {
                    progressBarLayer.Minimum = 0;
                    progressBarLayer.Maximum = this.ExportLayers.Count;

                    int k = 0;

                    foreach (KeyValuePair <int, ILayer> kvp in this.ExportLayers)
                    {
                        progressBarLayer.Value = ++k;

                        labelLayerName.Text = string.Format("Exporting Layer: {0} ({1} of {2})", kvp.Value.Name, k, this.ExportLayers.Count);
                        labelLayerName.Refresh();

                        DialogResult result = DialogResult.OK;

                        try
                        {
                            int srid = -1;

                            try
                            {
                                exporter.FindSRID(kvp.Value);
                            }
                            catch (InvalidSpatialReferenceException)
                            {
                            }
                            catch (Exception)
                            {
                                throw;
                            }



                            if (srid.Equals(-1))
                            {
                                result = MessageBox.Show(
                                    string.Format(
                                        "Spatial Reference: {0} ({1}) does not exist in spatialite.\n Do you wish to look up as EPSG?",
                                        kvp.Value.GetSpatialReference().Name,
                                        srid)
                                    , "Export to Spatialite", MessageBoxButtons.YesNo);

                                if (result == DialogResult.Yes)
                                {
                                    srid = exporter.FindSRID(kvp.Value, "epsg");

                                    if (srid.Equals(-1))
                                    {
                                        MessageBox.Show(
                                            string.Format(
                                                "Spatial Reference: {0} ({1}) does not exist in spatialite as EPSG either.  :( ",
                                                kvp.Value.GetSpatialReference().Name,
                                                srid)
                                            , "Export to Spatialite", MessageBoxButtons.OK);

                                        result = DialogResult.Cancel;
                                    }
                                    else
                                    {
                                        result = MessageBox.Show(
                                            string.Format(
                                                "Spatial Reference: {0} ({1}) does exist in spatialite as EPSG!",
                                                kvp.Value.GetSpatialReference().Name,
                                                srid)
                                            , "Export to Spatialite", MessageBoxButtons.OK);
                                    }
                                }
                            }

                            if (result == DialogResult.OK)
                            {
                                progressBarOperation.Value   = 0;
                                progressBarOperation.Minimum = 0;

                                exporter.StatusMessageEvent      += new SpatialiteExporter.StatusMessageDelegate(exporter_StatusMessageEvent);
                                exporter.AttributeExportProgress += new SpatialiteExporter.ExportProgressChanged(exporter_AttributeExportProgress);
                                exporter.GeometryExportProgress  += new SpatialiteExporter.ExportProgressChanged(exporter_GeometryExportProgress);
                                exporter.GeometryReadProgress    += new SpatialiteExporter.ExportProgressChanged(exporter_GeometryReadProgress);
                                exporter.AttributeReadProgress   += new SpatialiteExporter.ExportProgressChanged(exporter_AttributeReadProgress);


                                exporter.Export(kvp.Value, ((IFeatureLayer)(kvp.Value)).FeatureClass.ToOIDList(), srid, 1000);

                                // exporter.Export(kvp.Value, srid);

                                exporter.AttributeExportProgress -= exporter_AttributeExportProgress;
                                exporter.GeometryExportProgress  -= exporter_GeometryExportProgress;
                                exporter.GeometryReadProgress    -= exporter_GeometryReadProgress;
                                exporter.AttributeReadProgress   -= exporter_AttributeReadProgress;

                                MessageBox.Show(string.Format("Export to '{0}' is complete.", path), "Export to Spatialite.", MessageBoxButtons.OK);
                            }
                        }
                        catch (InvalidSpatialReferenceException ex)                        {
                            string message = "Sqlite/Spatialite Exception: \n\n{0}\n\n";

                            MessageBox.Show(string.Format(message, ex.Message),
                                            "Export To Spatialite Exception",
                                            MessageBoxButtons.OK);
                        }
                        catch (Exception)
                        {
                            throw;
                        }
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                isExporting = false;
                this.StopExportingEvent(this, new EventArgs());
            }
        }
示例#6
0
        private void LoadFeatureclass(IFeatureClass featureclass)
        {
            try
            {
                SpatialLiteDB db = new SpatialLiteDB(this.DatabasePath);
                db.Open();

                string sql = "SELECT AsBinary(" + Table.GeometryColumnName + ") as wkb,* FROM " + this.TableName;

                using (SQLiteCommand command = new SQLiteCommand(sql, db.SQLiteDatabaseConn))
                {
                    using (SQLiteDataAdapter adapter = new SQLiteDataAdapter(command))
                    {
                        System.Data.DataTable table = new System.Data.DataTable();
                        adapter.Fill(table);

                        IFeatureCursor cursor = featureclass.Insert(true);

                        foreach (DataRow row in table.Rows)
                        {
                            IFeatureBuffer feature = featureclass.CreateFeatureBuffer();

                            for (int i = 0; i < feature.Fields.FieldCount; i++)
                            {
                                IField field = feature.Fields.get_Field(i);
                                Debug.WriteLine("FieldName=" + field.Name);
                                if (field.Type != esriFieldType.esriFieldTypeGeometry
                                    & !field.Name.Equals("wkb", StringComparison.CurrentCultureIgnoreCase)
                                    & !field.Name.Equals("name", StringComparison.CurrentCultureIgnoreCase)
                                    & !field.Name.Equals(featureclass.OIDFieldName, StringComparison.CurrentCultureIgnoreCase))
                                {
                                    try
                                    {
                                        Debug.WriteLine("Spatialite Field Value: " + row[field.Name].ToString());
                                        feature.set_Value(i, row[field.Name]);
                                    }
                                    catch (Exception ex)
                                    {
                                        Trace.WriteLine(ex.StackTrace);
                                    }
                                }
                                else if (field.Type == esriFieldType.esriFieldTypeGeometry)
                                {
                                    byte[]    wkb = (byte[])row["wkb"];
                                    int       bytesRead;
                                    object    missing = Type.Missing;
                                    IGeometry outGeometry;


                                    object byteArrayObject = row["wkb"];

                                    IGeometryFactory3 factory = new GeometryEnvironment() as IGeometryFactory3;

                                    factory.CreateGeometryFromWkbVariant(byteArrayObject, out outGeometry, out bytesRead);

                                    if (outGeometry != null)
                                    {
                                        try
                                        {
                                            feature.Shape = outGeometry;
                                        }
                                        catch (Exception ex)
                                        {
                                            Trace.WriteLine(ex.StackTrace);
                                        }
                                    }
                                }
                            }

                            cursor.InsertFeature(feature);
                        }
                    }
                }
                db.Close();
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.StackTrace);
                throw;
            }
        }