ThinkGeo Cloud
ThinkGeo UI Controls
ThinkGeo Open Source
Help and Support
External Resources
ThinkGeo Cloud
ThinkGeo UI Controls
ThinkGeo Open Source
Help and Support
External Resources
using Microsoft.Win32; using System.Collections.Generic; using System.ComponentModel; using System.Configuration; using System.Data.SQLite; using System.Diagnostics; using System.IO; using System.Linq; using System.Windows; using ThinkGeo.MapSuite.Core; namespace ShapefileToSQLiteSample { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { private BackgroundWorker backgroundWorker; public MainWindow() { InitializeComponent(); backgroundWorker = new BackgroundWorker(); backgroundWorker.DoWork += backgroundWorker_DoWork; backgroundWorker.RunWorkerCompleted += backgroundWorker_RunWorkerCompleted; } void backgroundWorker_DoWork(object sender, DoWorkEventArgs e) { // Create SQLite database. string[] args = e.Argument as string[]; string databaseName = args[1]; if (File.Exists(databaseName)) { File.Delete(databaseName); } SqliteFeatureSource.CreateDatabase(databaseName); // Create SQLite table based on the shape file. ShapeFileFeatureSource shapeFileFeatureSource = new ShapeFileFeatureSource(args[0], ShapeFileReadWriteMode.ReadOnly); shapeFileFeatureSource.Open(); // These columns are auto generated when create SQLite tables. var excludeColumns = new[] { "id", "geometry", "name" }; IEnumerable<SqliteColumn> sqliteColumns = shapeFileFeatureSource.GetColumns() .Where(p => !excludeColumns.Contains(p.ColumnName.ToLowerInvariant())) .Select(p => new SqliteColumn() { ColumnName = p.ColumnName }); string connectingString = string.Format("Data Source={0};Version=3;", databaseName); SqliteFeatureSource.CreateTable(connectingString, "table_name", sqliteColumns, GeographyUnit.DecimalDegree); // Create spatial index. using (SQLiteConnection sqliteConnection = new SQLiteConnection(connectingString)) { sqliteConnection.Open(); // 4326=WGS84 SqliteFeatureSource.CreateSpatialIndex(sqliteConnection, "table_name", "geometry", 4326); } // Insert feature to SQLite table. SqliteFeatureSource sqliteFeatureSource = new SqliteFeatureSource(connectingString, "table_name", "id", "geometry"); sqliteFeatureSource.Open(); sqliteFeatureSource.BeginTransaction(); for (int i = 1; i < shapeFileFeatureSource.GetCount(); i++) { Feature feature = shapeFileFeatureSource.GetFeatureById(i.ToString(), ReturningColumnsType.AllColumns); sqliteFeatureSource.AddFeature(feature); } sqliteFeatureSource.CommitTransaction(); sqliteFeatureSource.Close(); shapeFileFeatureSource.Close(); } void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { busyIndicator.Visibility = Visibility.Collapsed; btnBrowse.IsEnabled = true; btnOk.IsEnabled = true; btnOutputBrowse.IsEnabled = true; if (MessageBox.Show("Convert compelted. Do you want to open the folder?", "Confirm", MessageBoxButton.YesNo) == MessageBoxResult.Yes) { Process.Start(Path.GetDirectoryName(txtOutputFileName.Text)); } } private void btnBrowse_Click(object sender, RoutedEventArgs e) { if (!backgroundWorker.IsBusy) { OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Filter = "(*.shp)|*.shp"; if (openFileDialog.ShowDialog() == true) { string selectedShapeFileName = openFileDialog.FileName; txtSelectedShapeFileName.Text = selectedShapeFileName; btnOutputBrowse.IsEnabled = true; txtOutputFileName.Text = Path.Combine(Path.GetDirectoryName(selectedShapeFileName), Path.GetFileNameWithoutExtension(selectedShapeFileName) + ".sqlite"); } } } private void btnOk_Click(object sender, RoutedEventArgs e) { string selectedShapeFileName = txtSelectedShapeFileName.Text; string outPutFileName = txtOutputFileName.Text; if (!string.IsNullOrEmpty(selectedShapeFileName) && !string.IsNullOrEmpty(outPutFileName) && !backgroundWorker.IsBusy) { busyIndicator.Visibility = Visibility.Visible; btnBrowse.IsEnabled = false; btnOk.IsEnabled = false; btnOutputBrowse.IsEnabled = false; // pass the shape file name to background thread. backgroundWorker.RunWorkerAsync(new[] { selectedShapeFileName, outPutFileName }); } } private void btnOutputBrowse_Click(object sender, RoutedEventArgs e) { if (!backgroundWorker.IsBusy && !string.IsNullOrEmpty(txtSelectedShapeFileName.Text)) { System.Windows.Forms.FolderBrowserDialog dialog = new System.Windows.Forms.FolderBrowserDialog(); if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) { txtOutputFileName.Text = Path.Combine(dialog.SelectedPath, Path.GetFileNameWithoutExtension(txtSelectedShapeFileName.Text) + ".sqlite"); } } } private void btnCancel_Click(object sender, RoutedEventArgs e) { Close(); } } }