Exemple #1
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;
            }
        }
Exemple #2
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;
            }
        }
Exemple #3
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;
            }
        }