public IWritableFeatureProvider ConstructTargetProvider(Type oidType, IGeometryFactory geometryFactory,
                                                                ICoordinateSystemFactory csFactory,
                                                                FeatureDataTable schemaTable)
        {
            if (oidType == typeof(UInt16))
            {
                oidType = typeof(Int16);
            }
            else if (oidType == typeof(UInt32))
            {
                oidType = typeof(Int32);
            }
            else if (oidType == typeof(UInt64))
            {
                oidType = typeof(Int64);
            }

            _targetProviderOidType = oidType;

            Type typ = typeof(PostGisProvider <>);

            _specializedType = typ.MakeGenericType(oidType);

            header("Construct PostGis target provider\n" +
                   "* Author Felix Obermaier 2009\n" +
                   "* Ingenieurgruppe IVV GmbH & Co. KG\n" +
                   "* http://www.ivv-aachen.de");

            string connectionString = GetValue(
                "Please enter the connection string for the source database file.",
                Settings.Default.SourceConnectionString);

            string schema = GetValue("Please enter the schema name",
                                     Settings.Default.SourceSchema);

            string tableName = GetValue("Please enter the table name.", null);

            //PostGisProviderStatic.CreateDataTable<oidType.GetType()>( schemaTable, schema, tableName, connectionString)
            Type       pgpstatic         = typeof(PostGisProviderStatic);
            MethodInfo miCreateDataTable = pgpstatic.GetMethod(
                "CreateDataTable",
                BindingFlags.Public | BindingFlags.Static,
                null,
                new[] { typeof(FeatureDataTable), typeof(string), typeof(string), typeof(string), typeof(string) },
                null);

            MethodInfo miCreateDataTableGeneric = miCreateDataTable.MakeGenericMethod(new[] { oidType });

            miCreateDataTableGeneric.Invoke(
                null,
                new object[]
                { schemaTable, schema, tableName, connectionString, PostGisProviderStatic.DefaultGeometryColumnName });

            _targetProvider =
                (IWritableFeatureProvider)
                Activator.CreateInstance(_specializedType,
                                         schemaTable.GeometryFactory,
                                         connectionString, schema, tableName,
                                         schemaTable.Columns[0].ColumnName,
                                         PostGisProviderStatic.DefaultGeometryColumnName,
                                         new GeometryServices().CoordinateTransformationFactory);

            _targetProvider.Open();
            return(_targetProvider);
        }
Exemplo n.º 2
0
        private void DoConversion(ProviderItem input, IEnumerable <ProcessorItem> processors, ProviderItem output)
        {
            using (IConfigureFeatureSource csource = (IConfigureFeatureSource)Activator.CreateInstance(input.Builder))
            {
                IFeatureProvider psource    = csource.ConstructSourceProvider(_geometryServices);
                Type             srcOidType = GetTypeParamsOfImplementedInterface(psource.GetType(), typeof(IFeatureProvider <>))[0];
                List <IProcessFeatureDataRecords> realProcessors = new List <IProcessFeatureDataRecords>();

                foreach (ProcessorItem pi in processors)
                {
                    realProcessors.Add((IProcessFeatureDataRecords)Activator.CreateInstance(pi.ProcessorType));
                }

                FeatureDataRecordProcessor processChain = null;

                foreach (IProcessFeatureDataRecords processor in realProcessors)
                {
                    processChain = Equals(processChain, null)
                                       ? processor.Processor
                                       : ((IEnumerable <IFeatureDataRecord> o, ref int i) =>
                                          processor.Processor(processChain(o, ref i), ref i));
                }

                processChain = processChain ??
                               new FeatureDataRecordProcessor((IEnumerable <IFeatureDataRecord> o, ref int i) => o);

                if (!psource.IsOpen)
                {
                    psource.Open();
                }

                FeatureQueryExpression exp         = csource.ConstructSourceQueryExpression();
                FeatureDataTable       sourceModel = psource.CreateNewTable();
                int index = sourceModel.Columns.IndexOf(sourceModel.PrimaryKey[0]);
                IEnumerable <IFeatureDataRecord> sourceRecords = processChain(psource.ExecuteFeatureQuery(exp), ref index);

                //jd: TODO: need to test what happens if the IFeatureDataRecord shape is changed by the processor chain

                IConvertData converter = null;

                /* Some Data Providers do not respect the oidType param passed in.
                 * For instance Shapefile will always be IWritableFeatureProvider<UInt32>
                 * so we need to make sure we can coerce OID values  */



                using (
                    IConfigureFeatureTarget ctarget =
                        (IConfigureFeatureTarget)Activator.CreateInstance(output.Builder))
                {
                    Type oidType = csource.OidType;



                    using (IWritableFeatureProvider ptarget =
                               ctarget.ConstructTargetProvider(oidType, sourceModel.GeometryFactory,
                                                               _geometryServices.CoordinateSystemFactory,
                                                               sourceModel))
                    {
                        if (!ptarget.IsOpen)
                        {
                            ptarget.Open();
                        }

                        converter = GetConverter(csource.OidType, ctarget.OidType, sourceModel.NewRow(), index, sourceModel.GeometryFactory);



                        Console.WriteLine("Beginning Import.");
                        List <FeatureDataRow> features = new List <FeatureDataRow>();
                        int count = 0;
                        foreach (IFeatureDataRecord fdr in sourceRecords)
                        {
                            try
                            {
                                features.Add(converter.ConvertRecord(fdr));
                                if (++count % 100 == 0)
                                {
                                    ptarget.Insert(features);
                                    features.Clear();
                                }
                            }
                            catch (GeometryInvalidException ex)
                            {
                                Console.WriteLine("An Error Occured : " + ex.Message);
                                continue;
                            }
                        }

                        if (features.Count > 0)
                        {
                            ptarget.Insert(features);
                        }

                        count += features.Count;

                        features = null;

                        ptarget.Close();
                        Console.WriteLine(string.Format("{0} records processed", count));

                        ctarget.PostImport();
                    }
                }
            }

            Console.WriteLine("Finished");
        }
        public IWritableFeatureProvider ConstructTargetProvider(Type oidType, IGeometryFactory geometryFactory,
                                                                ICoordinateSystemFactory csFactory,
                                                                FeatureDataTable schemaTable)
        {
            if (oidType == typeof (UInt16))
                oidType = typeof (Int16);
            else if (oidType == typeof (UInt32))
                oidType = typeof (Int32);
            else if (oidType == typeof (UInt64))
                oidType = typeof (Int64);

            Type typ = typeof (MsSqlServer2008Provider<>);
            _specializedType = typ.MakeGenericType(oidType);
            _targetProviderOidType = oidType;

            Console.WriteLine(
                "Please enter the connection string for the target database server. Press enter to use the one below.(Remember 'Connection Timeout=0' for large datasets.)");
            Console.WriteLine(Settings.Default.TargetConnectionString);


            string connectionString = Console.ReadLine();
            if (connectionString == "")
                connectionString = Settings.Default.TargetConnectionString;
            else
                Settings.Default.TargetConnectionString = connectionString;
            Settings.Default.Save();

            Console.WriteLine("Please enter the schema for the table.");
            string schemaName = Console.ReadLine();

            Console.WriteLine("Please enter the table name.");
            string tableName = Console.ReadLine();

            _targetProvider = (IWritableFeatureProvider) _specializedType.GetMethod(
                                                             "Create",
                                                             BindingFlags.Public | BindingFlags.Static,
                                                             null,
                                                             CallingConventions.Standard,
                                                             new[]
                                                                 {
                                                                     typeof (string), typeof (IGeometryFactory),
                                                                     typeof (string), typeof (string),
                                                                     typeof (FeatureDataTable)
                                                                 }, null)
                                                             .Invoke(null,
                                                                     new object[]
                                                                         {
                                                                             connectionString, geometryFactory,
                                                                             schemaName,
                                                                             tableName, schemaTable
                                                                         });

            _targetProvider.Open();
            return _targetProvider;
        }
        public IWritableFeatureProvider ConstructTargetProvider(Type oidType, IGeometryFactory geometryFactory,
                                                                ICoordinateSystemFactory csFactory,
                                                                FeatureDataTable schemaTable)
        {
            if (oidType == typeof (UInt16))
                oidType = typeof (Int16);
            else if (oidType == typeof (UInt32))
                oidType = typeof (Int32);
            else if (oidType == typeof (UInt64))
                oidType = typeof (Int64);

            _targetProviderOidType = oidType;

            Type typ = typeof (PostGisProvider<>);
            _specializedType = typ.MakeGenericType(oidType);

            header("Construct PostGis target provider\n" +
                   "* Author Felix Obermaier 2009\n" +
                   "* Ingenieurgruppe IVV GmbH & Co. KG\n" +
                   "* http://www.ivv-aachen.de");

            string connectionString = GetValue(
                "Please enter the connection string for the source database file.",
                Settings.Default.SourceConnectionString);

            string schema = GetValue("Please enter the schema name",
                                     Settings.Default.SourceSchema);

            string tableName = GetValue("Please enter the table name.", null);

            //PostGisProviderStatic.CreateDataTable<oidType.GetType()>( schemaTable, schema, tableName, connectionString)
            Type pgpstatic = typeof (PostGisProviderStatic);
            MethodInfo miCreateDataTable = pgpstatic.GetMethod(
                "CreateDataTable",
                BindingFlags.Public | BindingFlags.Static,
                null,
                new[] {typeof (FeatureDataTable), typeof (string), typeof (string), typeof (string), typeof (string)},
                null);

            MethodInfo miCreateDataTableGeneric = miCreateDataTable.MakeGenericMethod(new[] {oidType});

            miCreateDataTableGeneric.Invoke(
                null,
                new object[]
                    {schemaTable, schema, tableName, connectionString, PostGisProviderStatic.DefaultGeometryColumnName});

            _targetProvider =
                (IWritableFeatureProvider)
                Activator.CreateInstance(_specializedType,
                                         schemaTable.GeometryFactory,
                                         connectionString, schema, tableName,
                                         schemaTable.Columns[0].ColumnName,
                                         PostGisProviderStatic.DefaultGeometryColumnName,
                                         new GeometryServices().CoordinateTransformationFactory);

            _targetProvider.Open();
            return _targetProvider;
        }
        public IWritableFeatureProvider ConstructTargetProvider(Type oidType, IGeometryFactory geometryFactory,
                                                                ICoordinateSystemFactory csFactory,
                                                                FeatureDataTable schemaTable)
        {
            if (oidType == typeof(UInt16))
            {
                oidType = typeof(Int16);
            }
            else if (oidType == typeof(UInt32))
            {
                oidType = typeof(Int32);
            }
            else if (oidType == typeof(UInt64))
            {
                oidType = typeof(Int64);
            }

            Type typ = typeof(MsSqlServer2008Provider <>);

            _specializedType       = typ.MakeGenericType(oidType);
            _targetProviderOidType = oidType;

            Console.WriteLine(
                "Please enter the connection string for the target database server. Press enter to use the one below.(Remember 'Connection Timeout=0' for large datasets.)");
            Console.WriteLine(Settings.Default.TargetConnectionString);


            string connectionString = Console.ReadLine();

            if (connectionString == "")
            {
                connectionString = Settings.Default.TargetConnectionString;
            }
            else
            {
                Settings.Default.TargetConnectionString = connectionString;
            }
            Settings.Default.Save();

            Console.WriteLine("Please enter the schema for the table.");
            string schemaName = Console.ReadLine();

            Console.WriteLine("Please enter the table name.");
            string tableName = Console.ReadLine();

            _targetProvider = (IWritableFeatureProvider)_specializedType.GetMethod(
                "Create",
                BindingFlags.Public | BindingFlags.Static,
                null,
                CallingConventions.Standard,
                new[]
            {
                typeof(string), typeof(IGeometryFactory),
                typeof(string), typeof(string),
                typeof(FeatureDataTable)
            }, null)
                              .Invoke(null,
                                      new object[]
            {
                connectionString, geometryFactory,
                schemaName,
                tableName, schemaTable
            });

            _targetProvider.Open();
            return(_targetProvider);
        }