Select to view content in your preferred language

Read XY Coordinates from CSV and create Point Feature/ insert into GeoDB

711
7
03-14-2011 06:07 AM
SebastianKrings
Frequent Contributor
Hello,

I'm trying to read coordinates from a CSV File and insert them into my personal geodatabase.
I have created a layer ("Koordinaten") in my geoDB. There I want to add one or more points.

I thought of an insert query but didn find any material.
Now I tried to merge some code-snipplets I found:

public void InsertRows()
        {
            try
            {
                String strMDBFile = @"\\vmware-host\Shared Folders\SharedFolder\GeoData\Detmold\DetmoldDB.mdb";

                Type factoryType = Type.GetTypeFromProgID("esriDataSourcesGDB.FileGDBWorkspaceFactory");
                IWorkspaceFactory aoiAccFact = (IWorkspaceFactory)Activator.CreateInstance(factoryType);

                IFeatureWorkspace aoiAccWsp = (IFeatureWorkspace)aoiAccFact.OpenFromFile(strMDBFile, 0);
                IFeatureClass aoiFeatClass = aoiAccWsp.OpenFeatureClass("Koordinaten");

                IFeatureClassWrite aoiFCW = (IFeatureClassWrite)aoiFeatClass;

                IPoint p1 = new PointClass();
                p1.X = 100;
                p1.Y = 100;

                aoiFCW.WriteFeature((IFeature)p1);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.StackTrace);
            }
        }


Buzt when executing
IFeatureWorkspace aoiAccWsp = (IFeatureWorkspace)aoiAccFact.OpenFromFile(strMDBFile, 0);


I get an error:

Ausnahmemeldung "Beim Aufruf einer COM-Komponente wurde ein HRESULT E_FAIL-Fehler zurückgegeben."
Ausnahmetyp "System.Runtime.InteropServices.COMException"



But the error is not my main-intention I think. Primary I want to know wether I'm on the right way or there is a better one to achieve my aim.

Thanks for any help.
0 Kudos
7 Replies
JohnHauck
Frequent Contributor
Double-check that you're getting access to a valid license. If you're using 10 make sure you add your product binding code also.

You look like you're on the right path. Insert cursors would be a good way to go. Here are a few links I hope you find useful:

Creating Features

Geodatabase API best practices

Geodatabase Overview
0 Kudos
SebastianKrings
Frequent Contributor
Hi,

what is it about the product binding code?
Is there something I have to do programmatically?
I have a license for ArcEditor. As per ArcGIS Administrator all Products I installed are registered.

Thanks.
0 Kudos
SebastianKrings
Frequent Contributor
oh happpy

I have found the fault.

When using a personal GDB I have to use

 Type factoryType = Type.GetTypeFromProgID("esriDataSourcesGDB.AccessWorkspaceFactory");


instead of

 Type factoryType = Type.GetTypeFromProgID("esriDataSourcesGDB.FileGDBWorkspaceFactory");


I always thought that a FileGDB is a DB consisting of one single file.


So but after this H dircetly have another exception.
It appears when calling

 Type factoryType = Type.GetTypeFromProgID("esriDataSourcesGDB.FileGDBWorkspaceFactory");


The exception is
Update oder CancelUpdate ohne AddNew oder Edit.

I belive I have to start an edit-session, ok.
As described in the ESRI samples, you have to start an edit session every time when its a versioned future.
Do I have one? Didnt know.

And if I start such an edit session
workspaceEdit.StartEditing(false);
                workspaceEdit.StartEditOperation();

(doesnt matter whether EditOperation is also called)
an Exception will be thrown calling "No current dataset"

Thanks for help.
0 Kudos
LukeBadgerow
Deactivated User
I hate multiple posting this, but it looks like you've managed to make happen what I've been trying to get accomplished.  I'm stuck getting the following error when I try and create the feature class:

(http://forums.arcgis.com/threads/25560-IFeatureWorkspace.CreateFeatureClass()-error-(C-))

Attempted to read or write protected memory.  This is often an indication that other memory is corrupt

thing is that if I break my code just after the gdb is created I am able to use the system to create the feature class, so I know the memory is actually fine.  my code for creation of the fc follows:
        public void LoadPoints(IArray parsedcsv)
        {
            String jobnumber = "job_" + GetJobNumFromCSV(this.filename);

            Type factoryType = Type.GetTypeFromProgID("esriDataSourcesGDB.FileGDBWorkspaceFactory");
            IWorkspaceFactory2 workspaceFactory = (IWorkspaceFactory2)Activator.CreateInstance(factoryType);
            IWorkspace ws = workspaceFactory.OpenFromFile(outlocation + jobnumber + ".gdb", 0);
            IFeatureWorkspace gdb = (IFeatureWorkspace)ws;
            for(int i= 0; i < parsedcsv.Count; i++)
            //foreach (String[] record in parsedcsv)
            {
                String[] record = (String[])parsedcsv.get_Element(i);
                String outputlocation = "C:\\Documents and Settings\\lbadgerow\\Desktop\\ParseCSVTEST\\";
                String outfile = "";
                if (record.Length > 4)
                {
                    String pointtype = record.GetValue(4).ToString(); //hard coded value for column of type
                    String rec = record.ToString();
                    switch (pointtype)
                    {
                        case ("MP"):
                            outfile = outputlocation + pointtype + ".txt";
                            StreamWriting(pointtype, outfile, record, gdb);
                            break;
                        case ("BV"):
                            outfile = outputlocation + pointtype + ".txt";
                            StreamWriting(pointtype, outfile, record, gdb);
                            break;
                        case ("WT"):
                            outfile = outputlocation + pointtype + ".txt";
                            StreamWriting(pointtype, outfile, record, gdb);
                            break;
                        case ("VL"):
                            outfile = outputlocation + pointtype + ".txt";
                            StreamWriting(pointtype, outfile, record, gdb);
                            break;
                        case ("FH"):
                            outfile = outputlocation + pointtype + ".txt";
                            StreamWriting(pointtype, outfile, record, gdb);
                            break;
                        case ("TP"):
                            outfile = outputlocation + pointtype + ".txt";
                            StreamWriting(pointtype, outfile, record, gdb);
                            break;
                    }
                }
                else
                {
                    //IN THE EVENT THAT THE 4TH COLUMN IS NULL, ASSUME THAT IT'S A WATER TRACE POINT.
                    String pointtype = "WT";
                    outfile = outputlocation + pointtype + ".txt";
                    StreamWriting(pointtype, outfile, record, gdb);
                }
            }
        }

        private IFeatureClass CreateFeatureClass(String pointtype, IFeatureWorkspace workspace)
        {
            IFeatureClass outfc;
            IFields outfields = CreateFields();
            
            var CLSID = new UIDClass();
            CLSID.Value = "esriGeodatabase.Feature";

            try
            {

                outfc = workspace.CreateFeatureClass(pointtype, outfields, CLSID, null, esriFeatureType.esriFTSimple, "SHAPE", "");

            }
            catch (Exception exc)
            {
                MessageBox.Show(String.Format("Error {0}", exc.Message));
               
                throw new Exception(String.Format("Error {0}", exc.Message));
            }
            return outfc;
        }

        private void StreamWriting(String pointtype, String outfile, String[] record, IFeatureWorkspace workspace)
        {
            String finalstring = "";
           
            int recodLength = record.Length;
           
            IFeatureClass outfc = CreateFeatureClass(pointtype, workspace);
                        
            for (int a = 0; a < record.Length; a++)
            {
                if (a > 0)
                {
                    String value = record.ElementAt(a);
                    if (finalstring.Equals(""))
                    {
                        if (!value.Equals(""))
                        {
                            finalstring = value + ",";
                        }
                    }
                    else
                    {
                        if (!value.Equals(""))
                        {
                            finalstring += value + ",";
                        }
                    }
                }
            }

            finalstring.TrimEnd(',');

            if (!File.Exists(outfile))
            {

                StreamWriter streamWriter = new StreamWriter(outfile);
                streamWriter.WriteLine(finalstring);
                streamWriter.Close();
            }
            else
            {
                //append to feature class
                StreamWriter streamWriter = File.AppendText(outfile);
                streamWriter.WriteLine(finalstring);
                streamWriter.Close();
            }
        }


I did find the following information about an issue with the IList<T> and ICollection<T> interfaces causing issues in the DOTNET 2.0 framework:

http://support.microsoft.com/kb/971030

But I would think that switching to using ESRI.ArcGIS.esriSystem.IArray would clear that up, unless IArray inherits from ICollection as well.

At this point (I've been on this hang up for 2+ weeks now) I'm waiting on getting my global account squared with the company that I'm working for now so that I can submit a support ticket, so ANY advice is going to be greatly appreciated.  I'd be extremely happy if someone found a simple typo in my code was the culprit.
0 Kudos
MichaelRobb
Honored Contributor
Python does this all in one step, it was shown at the 2011 Developer Summit
0 Kudos
SebastianKrings
Frequent Contributor
Hello,

I found a mistake in my last post.

I wrote:

So but after this H dircetly have another exception.
It appears when calling

Type factoryType = Type.GetTypeFromProgID("esriDataSourcesGDB.FileGDBWorkspaceFactory");


But this is still the same line as before.

but however there's no exception any more
I was on holiday for a week, so did nothing, an today it works fine, my point is insertet into the gdb.

thanks for your help
0 Kudos
LukeBadgerow
Deactivated User
Python does this all in one step, it was shown at the 2011 Developer Summit


I prototyped this with a python script.  fact is that I've had the python for this for a number of years, but now I'm winding it into an add-in so I need to get it into the framework.
0 Kudos