示例#1
0
        /// <summary>
        /// Tries to retrieve object of type <typeparamref name="TFeature"/>.
        /// If this is not possible, throws <see cref="NotSupportedException"/>/
        /// </summary>
        /// <typeparam name="TFeature">A type of the feature to retrieve.</typeparam>
        /// <param name="provider">A feature provider.</param>
        /// <returns>Feature of type <typeparamref name="TFeature"/>.</returns>
        /// <exception cref="NotSupportedException">If <paramref name="provider"/> doesn't support feature of type <typeparamref name="TFeature"/>.</exception>
        public static TFeature With <TFeature>(this IFeatureProvider provider)
        {
            Ensure.NotNull(provider, "model");

            TFeature feature;

            if (provider.TryWith(out feature))
            {
                return(feature);
            }

            throw Ensure.Exception.NotSupported(
                      "Feature provider '{0}' doesn't support feature '{1}'.",
                      provider.GetType().FullName,
                      typeof(TFeature).FullName
                      );
        }
示例#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");
        }