Techno Curry

I am an entrepreneur who believes that the true power of GIS is yet to be discovered in every day aspects of life. As one of the primary owners of 39 Degrees North (www.39dn.com) a GIS focused company that strives to be on the bleeding edge of technology , I believe in bringing GIS to the masses at affordable cost and time investments that can help the users to achieve great ROI.

I am also a travel , motorcycle, gardening , outdoors and cooking enthusiast when I find the time in my busy schedule. Cooking being one of my great passions I always find time to do that , I also enjoy having dinner parties where I cook up a storm to feed all my friends.

Overcoming lock / renaming issues when rebuilding a featureclass

Yet another old post from my previous blog

When you want to rebuild a featureclass programmatically that is in use from lets say a SQL table you have two options. One is to create a new featureclass and rename it to the new one after the old one is renamed to a different name. This requires exclusive locks that may not be available and cause the program to crash. The other option is to delete all the features through IFeatureClass interface which can be really time consuming.

There is another alternative, using direct SQL to truncate the three tables that are associated with a featureclass. This does not require any exclusive locks and can be done while the featureclass is in use. Also it is cleaner and much faster.

Below is the code to truncate a featureclass in C#

  public void TruncateFeatureclass(IFeatureClass pFeatClass, IWorkspace pWorkspaceSDE)
        {
           
            IDataset pDataset;
            pDataset = (IDataset)pFeatClass;
            IWorkspace pWorkspaceData;
            pWorkspaceData = pDataset.Workspace;

            string strFCName;
            strFCName = pDataset.Name;
            int lngPosDot;
            lngPosDot = strFCName.IndexOf(".");  //InStr(strFCName, ".");
            string strOwner;
            string strName;
            string strdbName;
            string ownDotName;
            strdbName = strFCName.Substring(0, lngPosDot);//Left(strFCName, lngPosDot - 1);
            ownDotName = strFCName.Substring(lngPosDot + 1, strFCName.Length - lngPosDot - 1);
            int lngPosDot2;
            lngPosDot2 = ownDotName.IndexOf(".");
            strOwner = ownDotName.Substring(0, lngPosDot2);

            strName = ownDotName.Substring(lngPosDot2 + 1, ownDotName.Length - lngPosDot2 - 1); //Mid(strFCName, lngPosDot + 1);

            IFeatureWorkspace pFeatWorkspace;
            pFeatWorkspace = (IFeatureWorkspace)pWorkspaceSDE;

            IQueryDef pQueryDef;
            ICursor pCursor;
            IRow pRow;
            pQueryDef = pFeatWorkspace.CreateQueryDef();
            pQueryDef.Tables = "sde_layers";
            pQueryDef.SubFields = "sde_layers.layer_id";
            pQueryDef.WhereClause = "sde_layers.owner='" + strOwner.ToUpper() + "' and sde_layers.table_name = '" + strName.ToUpper() + "'";
            pCursor = pQueryDef.Evaluate();
            pRow = pCursor.NextRow();

            long lngLayerId;
            lngLayerId = Convert.ToInt64(pRow.get_Value(0));

            pWorkspaceData.ExecuteSQL("TRUNCATE TABLE " + strFCName);
            pWorkspaceData.ExecuteSQL("TRUNCATE TABLE F" + lngLayerId);
            pWorkspaceData.ExecuteSQL("TRUNCATE TABLE S" + lngLayerId);
        }

Creating Theissen Polygons in ArcMap

Import from my old blog

Public Function CreateThiessenFC(pFeatureClass As IFeatureClass, pPoly As IPolygon, pFDS As IFeatureDataset) As IFeatureClass 'If (Not dbUtil.ResolveOutputFeatureClassName(txtOutput, sOutCat, sOutWSName, sOutDSName, sOutFCName, True)) Then ' If (dbUtil.GetErrorCode = 1) Then ' MsgBox dbUtil.GetErrorMessage, vbCritical ' End If ' Exit Sub 'End If 'Me.MousePointer = vbHourglass  Dim pGDS As IGeoDataset Set pGDS = pFeatureClass Dim pSR As ISpatialReference Set pSR = pGDS.SpatialReference  Dim pFields As IFields2 Set pFields = pFeatureClass.Fields  Dim pOIDField As IField Dim lOIDInx As Long lOIDInx = pFields.FindField(pFeatureClass.OIDFieldName) Set pOIDField = pFields.Field(lOIDInx)  Dim pOutFC As IFeatureClass 'Check the parameter of this function Set pOutFC = createDatasetFeatureClass(pFDS, "thiessen", esriFTSimple, esriGeometryPolygon, Nothing, Nothing, Nothing) 'This is the old one 'Set pOutFC = createDatasetFeatureClass(sOutCat, sOutWSName, sOutDSName, sOutFCName, esriGeometryPolygon, False, False, pSR)  Dim pTinEdit As ITinEdit Set pTinEdit = New Tin pTinEdit.InitNew pGDS.Extent  Dim pTinAdv As ITinAdvanced2 Set pTinAdv = pTinEdit  pTinEdit.AddFromFeatureClass pFeatureClass, Nothing, pOIDField, pOIDField, esriTinMassPoint  pTinEdit.Refresh ' get proper extent so default voronoi clip poly is ok   'Before converting to the theissen check for the bounding polygon   Dim pNodeCol As ITinNodeCollection Set pNodeCol = pTinEdit pNodeCol.ConvertToVoronoiRegions pOutFC, Nothing, pPoly, "", ""  'pTinEdit.SaveAs (sOutWSName & "\" & dbUtil.GetUniqueFileName(sOutWSName)) 'pTinEdit.StopEditing False  'AddFeatureLayer m_pApp, pOutFC  Set CreateThiessenFC = pOutFC  'Release the variables  Set pGDS = Nothing Set pSR = Nothing Set pFields = Nothing Set pOIDField = Nothing 'Set lOIDInx = Nothing Set pOutFC = Nothing Set pNodeCol = Nothing Set pNodeCol = Nothing  End Function