POST
|
I am trying to convert a dbf to a csv but come by this error when I hit certain characters (in this case á I think). I know there are many more.. Is there a way to force this to convert to utf-8? Is this the way to handle this? Any help is greatly appreciated! import arcpy, os, csv
masterTable = r"M:\Spelling_Project\Master_Table\FeaturesTable.dbf"
CSVFile = r"M:\Spelling_Project\Master_Table\FeaturesTable.csv"
fields = arcpy.ListFields(masterTable)
fieldNames = [field.name for field in fields]
with open(CSVFile,'w') as f:
dw = csv.DictWriter(f,fieldNames)
dw.writeheader()
with arcpy.da.SearchCursor(masterTable,fieldNames) as cursor:
for row in cursor:
dw.writerow(dict(zip(fieldNames,row)))
print row
print "converted " + masterTable + " to a CSV file!"
del row, cursor *EDIT* For anyone who will find this helpful, this is what I used to solve this. import arcpy, os, csv, codecs
def Utf8EncodeArray( oldArray ):
newArray = []
for element in oldArray:
if isinstance(element, unicode):
newArray.append(element.encode("utf-8"))
else:
newArray.append(element)
return newArray
masterTable = r"M:\Spelling_Project\Master_Table\FeaturesTable.dbf"
CSVFile = r"M:\Spelling_Project\Master_Table\FeaturesTable.csv"
fields = arcpy.ListFields(masterTable)
fieldNames = [field.name for field in fields]
with open(CSVFile, 'w') as f:
dw = csv.DictWriter(f,fieldNames)
dw.writeheader()
with arcpy.da.SearchCursor(masterTable,fieldNames) as cursor:
for row in cursor:
dw.writerow(dict(zip(fieldNames,Utf8EncodeArray(row))))
print "converted " + masterTable + " to a CSV file!"
del row, cursor Message was edited by: Chris Brannin
... View more
04-20-2015
02:11 PM
|
0
|
1
|
12712
|
POST
|
I want to loop through a folder and clip all feature classes by a AOI poly and write the results to an output folder. How to I also have the loop search all subfolders for vectors and raster feature classes as well? This will walk all directories you want, including GDB's. You can also add the if to remove directories you're not interested in crawling. for dirpath, dirnames, filenames in arcpy.da.Walk(in_workspace, datatype="FeatureClass",type="All"):
if "Remove_Folder" in dirnames:
dirnames.remove('Remove_Folder')
for filename in filenames:
if "{Field Name}" in [f.name for f in arcpy.ListFields(os.path.join(dirpath, filename))]:
arcpy.Clip_analysis(os.path.join(dirpath, filename), clip_features, out_feature_class, xy_tolerance)
... View more
07-02-2014
12:15 PM
|
0
|
0
|
1163
|
POST
|
I have a simple script that gets all layers in a gdb and uses them to clip a DEM. What's happening though is it starts the clipping but never finishes and after killing the process I am unable to delete the file because of some lock on the file. Any thoughts/ideas on what's causing this or how to fix it? Any help is greatly appreciated. for dirpath, dirnames, filenames in arcpy.da.Walk(clipLayers, datatype="FeatureClass", type="ALL"):
for filename in filenames:
if "ENG_NAME" in [f.name for f in arcpy.ListFields(os.path.join(dirpath, filename))]:
print "Clipping: ", filename
arcpy.env.overwriteOutput = True
arcpy.Clip_management(dem, "-180.000417 -56.000417 180.000425 61.000419", r"M:/Elevation_Masks/test_space/Country_Masks_Test/Country_Masks_Test.gdb/" + filename + "_Clip", os.path.join(dirpath, filename), "NULL", "ClippingGeometry")
... View more
07-02-2014
11:52 AM
|
0
|
0
|
339
|
POST
|
I don't see anywhere in the code you posted where you are creating a feature class, or a feature for that matter. Your code is drawing a point on the display. Unless you put this code in the active view's AfterDraw event, it will simply draw once and then disappear on the next refresh of the display because it's simply a graphic and is not permanent. If you want to create a new feature from the point and save it into a feature class then that is completely different than what you are doing in the code you posted. There are examples that show how to create new features in the developer samples and here on the forums. *EDIT - WORKING* I took a slightly different approach to this and it seems to be working in the dataview... Code is edited to reflect current working solution. Any suggestions to clean this up or make it better would still be greatly appreciated! Hello and thanks for the reply. I tried to post all of the code but it told me it was too many characters. I only posted the drawing part to see if I was going about the drawing correctly and to get suggestions on where to place it within the featureClass creation method. Can you point me to these samples, because I am creating the FC just fine but not with the symbol. I thought I was doing something wrong with my drawing. Simplified Code:
IFields fields = new FieldsClass();
IFieldsEdit fieldsEdit = (IFieldsEdit)fields;
IField oidField = new FieldClass();
IFieldEdit oidFieldEdit = (IFieldEdit)oidField;
oidFieldEdit.Name_2 = "OID";
oidFieldEdit.Type_2 = esriFieldType.esriFieldTypeOID;
fieldsEdit.AddField(oidField);
//create geometry definition and spatial reference for the featureclass
IGeometryDef geometryDef = new GeometryDefClass();
IGeometryDefEdit geometryDefEdit = (IGeometryDefEdit)geometryDef;
geometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPoint;
ISpatialReferenceFactory spatialRefFactory = new SpatialReferenceEnvironmentClass();
int coordinateSystemID = (int)esriSRGeoCSType.esriSRGeoCS_WGS1984;
ISpatialReference spatialReference = spatialRefFactory.CreateGeographicCoordinateSystem(coordinateSystemID);
IField geometryField = new FieldClass();
IFieldEdit geometryFieldEdit = (IFieldEdit)geometryField;
//field = new FieldClass();
geometryFieldEdit.Name_2 = "Shape";
geometryFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;
geometryFieldEdit.GeometryDef_2 = geometryDef;
fieldsEdit.AddField(geometryField);
//Create fields
IField A2Field = new FieldClass();
IFieldEdit A2FieldEdit = (IFieldEdit)A2Field;
A2FieldEdit.Name_2 = "ISO_A2";
A2FieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
A2FieldEdit.Length_2 = 254;
fieldsEdit.AddField(A2Field);
// Use IFieldChecker to create a validated fields collection
IFieldChecker fieldChecker = new FieldCheckerClass();
IEnumFieldError enumFieldError = null;
IFields validatedFields = null;
fieldChecker.ValidateWorkspace = (IWorkspace)fws;
fieldChecker.Validate(fields, out enumFieldError, out validatedFields);
IFeatureClass featureClass = fws.CreateFeatureClass(fcName, validatedFields, null, null , esriFeatureType.esriFTSimple, "Shape", "");
MessageBox.Show(fcName + " Has been created successfully!");
IFeatureClass fc = fws.OpenFeatureClass(fcName);
IFeatureLayer fl = new FeatureLayerClass();
fl.Name = fcName;
fl.FeatureClass = fc;
IMxDocument mxDoc = (IMxDocument)ArcMap.Application.Document;
IMap map = mxDoc.FocusMap;
map.AddLayer(fl);
IEnumLayer enumLayer = map.Layers;
ILayer layer = enumLayer.Next();
IFeatureLayer2 pointLayer = null;
while (layer != null)
{
if (layer.Name == fcName && layer is IFeatureLayer2)
{
pointLayer = layer as IFeatureLayer2;
}
layer = enumLayer.Next();
}
if (pointLayer == null)
{
MessageBox.Show("No layer!");
return;
}
try
{
//set coordinates
double xCoor = Convert.ToDouble(listVariants.SelectedItems[0].SubItems[6].Text);
double yCoor = Convert.ToDouble(listVariants.SelectedItems[0].SubItems[7].Text);
IFeature newPoint = pointLayer.FeatureClass.CreateFeature();
IPoint placePoint = new PointClass();
placePoint.PutCoords(yCoor, xCoor);
newPoint.Shape = placePoint;
IFeatureClass pointFClass = pointLayer.FeatureClass;
int fieldCOUNTRY = pointFClass.Fields.FindField("COUNTRY");
newPoint.Value[fieldCOUNTRY] = listVariants.SelectedItems[0].SubItems[5].Text;
newPoint.Store();
}
catch (Exception ex)
{
new MessageDialogClass().DoModal(ex.Source, ex.Message, "", "", ArcMap.Application.hWnd);
}
I'm very new to ArcObjects so any help suggestions are greatly appreciated! Cheers Chris
... View more
03-06-2014
05:47 AM
|
0
|
0
|
303
|
POST
|
You can try to Refresh your display after adding features to your featureclass. pMap.PartialRefresh(esriViewGeography, pLayer, null); for permanent Graphics on Map you can use AddGraphicToMap Snippet then Refresh your map. Regards, Thanks for the suggestions. I am still getting the same results. I wonder... Do I need to open an editing session? Cheers Chris
... View more
03-06-2014
04:54 AM
|
0
|
0
|
303
|
POST
|
Hello When I try to create a new featureClass I do not get the symbol created with the row(s) in the table. So I am trying to add the symbol as the featureClass is being created. Is this the right process or should the symbol be added on the creation of the featureClass? The coordinate flashes upon creation but does not stay!?
IPoint makePoint = new PointClass();
double xCoor = Convert.ToDouble(listVariants.SelectedItems[0].SubItems[6].Text);
double yCoor = Convert.ToDouble(listVariants.SelectedItems[0].SubItems[7].Text);
makePoint.PutCoords(xCoor, yCoor);
IMxDocument drawDoc = (IMxDocument)ArcMap.Application.Document;
IActiveView drawView = drawDoc.ActiveView;
IScreenDisplay drawDisplay = drawView.ScreenDisplay;
short screenCache = Convert.ToInt16(esriScreenCache.esriNoScreenCache);
drawDisplay.StartDrawing(drawDisplay.hDC, screenCache);
ESRI.ArcGIS.Display.RgbColorClass color = new ESRI.ArcGIS.Display.RgbColorClass();
color.Red = 55; color.Green = 100; color.Blue = 66;
ESRI.ArcGIS.Display.ISimpleMarkerSymbol simpleMarkerSymbol = new ESRI.ArcGIS.Display.SimpleMarkerSymbolClass();
simpleMarkerSymbol.Color = color;
simpleMarkerSymbol.Size = 8;
drawDisplay.SetSymbol(simpleMarkerSymbol as ISymbol);
drawDisplay.DrawPoint(makePoint);
drawDisplay.FinishDrawing();
Please, any suggestions are greatly appreciated! Cheers Chris
... View more
03-04-2014
10:09 AM
|
0
|
4
|
504
|
Online Status |
Offline
|
Date Last Visited |
11-11-2020
02:23 AM
|