ArcStrabo10.ArcStraboObject.MakingTextLabelGeoJsonFile C# (CSharp) Метод

MakingTextLabelGeoJsonFile() публичный Метод

access to the positive and negative layer and save them on the GeoJson File
public MakingTextLabelGeoJsonFile ( string dir ) : void
dir string
Результат void
        public void MakingTextLabelGeoJsonFile(string dir)
        {
            ///////////////////////////////////////////////////////////////
            //Setting current Map to access Layers, Feature Class and Features
            //build string builder to write on the ImageResultfile
            ///////////////////////////////////////////////////////////////
            string path = "";
            IMap map = ArcMap.Document.FocusMap;
            //GeoJson geoJson;
            IEnumLayer enumLayer = map.get_Layers(null, true);
            ILayer layer = enumLayer.Next();
            //((IDataset)layer).Workspace.
            IFeatureLayer featureLayer;

            try
            {
                while (layer != null)
                {
                    if (layer.Name == ArcStrabo10Extension.TextPositiveLabelLayerName || layer.Name == ArcStrabo10Extension.TextNegtiveLabelLayerName)
                    {
                        GeoJson geoJson = new GeoJson();
                        geoJson.featureInJson = new FeatureInJSON();
                        geoJson.featureInJson.displayFieldName = layer.Name;
                        geoJson.featureInJson.fieldAliases = new FieldAliases();

                        //////////////////////////Set Alias Feature of the Layer////////////////////
                        geoJson.featureInJson.fieldAliases.Mass_centerX = "Mass_centerX";
                        geoJson.featureInJson.fieldAliases.Mass_centerY = "Mass_centerY";
                        geoJson.featureInJson.fieldAliases.OBJECTID = "OBJECTID";
                        geoJson.featureInJson.fieldAliases.Orientation = "Orientation";
                        geoJson.featureInJson.fieldAliases.Susp_char_count = "Susp_char_count";
                        geoJson.featureInJson.fieldAliases.Susp_text = "Susp_text";
                        geoJson.featureInJson.fieldAliases.Text = "Text";
                        geoJson.featureInJson.fieldAliases.Char_count = "Char_count";
                        geoJson.featureInJson.fieldAliases.Filename = "Filename";

                        //////////////////////Set Fields of the current Layer///////////////////////

                        geoJson.featureInJson.fields = new Field_info[9];
                        setGeoJASONFeilds("OBJECTID", esriFieldType.esriFieldTypeOID, geoJson.featureInJson.fields[0]);
                        setGeoJASONFeilds("Text", esriFieldType.esriFieldTypeString, geoJson.featureInJson.fields[1]);
                        setGeoJASONFeilds("Char_count", esriFieldType.esriFieldTypeInteger, geoJson.featureInJson.fields[2]);
                        setGeoJASONFeilds("Orientation", esriFieldType.esriFieldTypeDouble, geoJson.featureInJson.fields[3]);
                        setGeoJASONFeilds("Filename", esriFieldType.esriFieldTypeString, geoJson.featureInJson.fields[4]);
                        setGeoJASONFeilds("Susp_text", esriFieldType.esriFieldTypeString, geoJson.featureInJson.fields[5]);
                        setGeoJASONFeilds("Susp_char_count", esriFieldType.esriFieldTypeInteger, geoJson.featureInJson.fields[6]);
                        setGeoJASONFeilds("Mass_centerX", esriFieldType.esriFieldTypeDouble, geoJson.featureInJson.fields[7]);
                        setGeoJASONFeilds("Mass_centerY", esriFieldType.esriFieldTypeDouble, geoJson.featureInJson.fields[8]);

                        //************************GeoProjectChange***SIIIIIIMA***********************************************************
                        if (layer.Name == ArcStrabo10Extension.TextPositiveLabelLayerName)
                            path = dir + "\\" + ArcStrabo10Extension.TextPositiveLabelLayerJSONFileName;
                        else if (layer.Name == ArcStrabo10Extension.TextNegtiveLabelLayerName)
                            path = dir + "\\" + ArcStrabo10Extension.TextNegtiveLabelLayerJSONFileName;

                        featureLayer = (IFeatureLayer)layer;
                        int count = featureLayer.FeatureClass.FeatureCount(null);
                        //for (int j = 1; j <= count; j++)
                        //{
                        IFeatureCursor featureCursor = featureLayer.FeatureClass.Search(null, false);// (null, enableRecycling);
                        IFeature iFeature = featureCursor.NextFeature();
                        int i = 0;
                        while (iFeature != null)
                        {

                            geoJson.featureInJson.features.Add(new Features());
                            geoJson.featureInJson.features[i].attributes = new Attributes();
                            geoJson.featureInJson.features[i].attributes.OBJECTID = int.Parse(iFeature.get_Value(0).ToString());
                            geoJson.featureInJson.features[i].geometry = new Strabo.Core.OCR.Geometry();
                            geoJson.featureInJson.features[i].geometry.rings[0, 0, 0] = iFeature.Extent.UpperLeft.X;
                            geoJson.featureInJson.features[i].geometry.rings[0, 0, 1] = iFeature.Extent.UpperLeft.Y;
                            geoJson.featureInJson.features[i].geometry.rings[0, 1, 0] = iFeature.Extent.UpperRight.X;
                            geoJson.featureInJson.features[i].geometry.rings[0, 1, 1] = iFeature.Extent.UpperRight.Y;
                            geoJson.featureInJson.features[i].geometry.rings[0, 2, 0] = iFeature.Extent.LowerRight.X;
                            geoJson.featureInJson.features[i].geometry.rings[0, 2, 1] = iFeature.Extent.LowerRight.Y;
                            geoJson.featureInJson.features[i].geometry.rings[0, 3, 0] = iFeature.Extent.LowerLeft.X;
                            geoJson.featureInJson.features[i].geometry.rings[0, 3, 1] = iFeature.Extent.LowerLeft.Y;
                            geoJson.featureInJson.features[i].geometry.rings[0, 4, 0] = iFeature.Extent.UpperLeft.X;
                            geoJson.featureInJson.features[i].geometry.rings[0, 4, 1] = iFeature.Extent.UpperLeft.Y;
                            iFeature = featureCursor.NextFeature();
                            i++;
                        }
                        geoJson.writeJsonFile(path);

                    }
                    layer = enumLayer.Next();
                }

            }
            catch (Exception e)
            {
                Log.WriteLine(e.Message);
            }
        }

Usage Example

        protected override void OnClick()
        {
            string straboPath = Environment.GetEnvironmentVariable(ArcStrabo10Extension.EnvironmentVariableSTRABO_HOME, EnvironmentVariableTarget.User);
            string tessPath = Environment.GetEnvironmentVariable(ArcStrabo10Extension.EnvironmentVariableTESS_DATA, EnvironmentVariableTarget.User);

            if (ArcStrabo10Extension.PathSet == false)
            {

                if (String.IsNullOrEmpty(straboPath) == true)
                {
                    MessageBox.Show(ArcStrabo10Extension.ErrorMsgNoStraboHome);
                    return;
                }
                if (String.IsNullOrEmpty(tessPath) == true)
                {
                    MessageBox.Show(ArcStrabo10Extension.ErrorMsgNoTess_Data);
                    return;
                }

                ////Initialize directories
                bool Initialize_straboPath_Correct = ArcStrabo10Extension.initialize_straboPath_directories(straboPath);

                if (Initialize_straboPath_Correct == false)
                {
                    MessageBox.Show(ArcStrabo10Extension.ErrorMsgNoStraboHomeWritePermission);
                    return;
                }
                ArcStrabo10Extension.PathSet = true;
            }

            #region Text Recognition
            ////Save Positive and Negative Layer and making GeoJason File
            ComboBoxLayerSelector layerNameCombo = ComboBoxLayerSelector.GetLayerNameComboBox();

            ////Select correct raster map layer
            RasterLayer rasterlayer = new RasterLayer();
            rasterlayer = ((RasterLayer)layerNameCombo.GetSelectedLayer());
            string input_data_source_directory;
            try
            {
                input_data_source_directory = rasterlayer.FilePath;
                //input_data_source_directory = "B:\\Users\\akshay anand\\strabo\\strabo\\data\\text_extraction\\Opensource_Afghanistan_Kabul_City_Center\\";
            }
            catch (Exception)
            {
                // Handle no input map error
                MessageBox.Show(ArcStrabo10Extension.ErrorMsgNoInputMap, "Input Map Error", MessageBoxButtons.OK);
                return;
            }

            ////Select language from combo box in toolbar
            ComboBoxLanguageSelector languageNameCombo = ComboBoxLanguageSelector.GetLanguageNameComboBox();
            string lng = languageNameCombo.Get_selected_language();
            if (lng == null)
            {
                MessageBox.Show(ArcStrabo10Extension.ErrorMsgNoInputLanguage, "Input Language Error", MessageBoxButtons.OK);
                return;
            }

            ////Set Log Directory Path
            Log.SetLogDir(ArcStrabo10Extension.Log_Path);
            Log.SetOutputDir(ArcStrabo10Extension.Log_Path);

            Log.WriteLine("MakingTextLabelGeoJsonFile Method Start SIMA");
            IMap map = ArcMap.Document.FocusMap;
            ArcStraboObject arcStraboObject = new ArcStraboObject();
            arcStraboObject.MakingTextLabelGeoJsonFile(ArcStrabo10Extension.Text_Result_Path);
            Log.WriteLine("MakingTextLabelGeoJsonFile Method Finish");

            ////Run TextExtraction Layer from Strabo.core and load raster Layer
            Log.WriteLine("textLayerExtract Medthod Start SIMA");
            arcStraboObject.textLayerExtract(input_data_source_directory, ArcStrabo10Extension.Text_Result_Path);
            Log.WriteLine("textLayerExtract Method Finish");

            Log.WriteLine("AddRasterLayer Method Start SIMA");
            arcStraboObject.AddRasterLayer(ArcStrabo10Extension.Text_Result_Path, ArcStrabo10Extension.TextLayerPNGFileName);
            Log.WriteLine("AddRasterLayer Method Finish");

            ////Run TextIdentifier Method
            Log.WriteLine("textIndentification Method Start SIMA");
            System.Windows.Forms.Cursor.Current = Cursors.WaitCursor;

            ///// Attempting to create cancel feature window
            //DialogResult result = MessageBox.Show("Text Recognition is running.", "Application Running", MessageBoxButtons.OKCancel);
            //if (result == DialogResult.Cancel)
            //{
            //    return;
            //}
            //else if (result == DialogResult.OK)

            arcStraboObject.textIndentification(ArcStrabo10Extension.Text_Result_Path + "\\", ArcStrabo10Extension.Intermediate_Result_Path + "\\", ArcStrabo10Extension.TextLayerPNGFileName);
            System.Windows.Forms.Cursor.Current = Cursors.Default;
            Log.WriteLine("textIndentification Method Finish");

            ////OCR Part
            Log.WriteLine("ExtractTextToGEOJSON Method Start SANJUALI");
            System.Windows.Forms.Cursor.Current = Cursors.AppStarting;

            //// Select language from combo box in toolbar
               // ComboBoxLanguageSelector languageNameCombo = ComboBoxLanguageSelector.GetLanguageNameComboBox();
               // string lng = languageNameCombo.Get_selected_language();
            if (lng == null)
            {
                MessageBox.Show(ArcStrabo10Extension.ErrorMsgNoInputLanguage, "Input Language Error", MessageBoxButtons.OK);
                return;
            }
            Strabo.Core.OCR.WrapperTesseract language = new Strabo.Core.OCR.WrapperTesseract(tessPath, lng);
            /// Strabo.Core.OCR.WrapperTesseract language = new Strabo.Core.OCR.WrapperTesseract(tessPath);
            language.ExtractTextToGEOJSON(ArcStrabo10Extension.Intermediate_Result_Path,ArcStrabo10Extension.Text_Result_Path,ArcStrabo10Extension.TesseractResultsJSONFileName);
            Log.WriteLine("ExtractTextToGEOJSON Method Finish");
            System.Windows.Forms.Cursor.Current = Cursors.Default;

            ////Add Polygon of OCR Layer
            Log.WriteLine("CreateFeatureClassWithFields Method Start SIMA");
            IWorkspace workspace = arcStraboObject.CreateShapefileWorkspace(ArcStrabo10Extension.Text_Result_Path);
            IFeatureWorkspace featureworkspace = (IFeatureWorkspace)workspace;
            string tesseDataPath = ArcStrabo10Extension.Text_Result_Path + "\\" + ArcStrabo10Extension.TesseractResultsJSONFileName;

            IFeatureClass featureClass = arcStraboObject.CreateFeatureClassWithFields(ArcStrabo10Extension.TextLayerOCRShapefile, featureworkspace, tesseDataPath);
            IFeatureLayer featureLayer = arcStraboObject.CreateFeatureLayer(featureClass);
            Log.WriteLine("CreateFeatureClassWithFields Method Finish");

            Log.WriteLine("AddPolygon Method Start");
            arcStraboObject.AddPolygon(featureLayer, featureworkspace, tesseDataPath);
            Log.WriteLine("AddPolygon Method Finish");

            Log.ArchiveLog();
            MessageBox.Show("Text recognition finished!", "Done", MessageBoxButtons.OK);
            #endregion
        }