|
POST
|
I am assuming that the picture in my last post is generally what you are looking for. As Dan Patterson mentioned, since the database exists you can't overwrite it. You can delete it with code or just use it. A line of code could be added to check for the existence of the database. As seen in your attached jpeg, you are getting a second error: object has no attribute 'SplitByAttributes_analysis' Split By Attributes was added at version 10.5 and Pro. Since you are at 10.1, we will need a different solution. I would suggest looping through your datasets and features and using Select Layer By Attribute with a where clause, something like: arcpy.SelectLayerByAttribute_management('FA_Box', 'NEW_SELECTION',
where_clause = "DRAWING_REFERENCE = 'C0500-GS-0117-01-EL-FA-A0-03-001-001'") If records are selected, then copy them to the appropriate database. Hope this helps.
... View more
02-06-2019
09:45 AM
|
0
|
0
|
1940
|
|
POST
|
The following code will split your features based on DRAWING_REFERENCE and place them in a geodatabase named with the feature class. From here you should be able to copy them into the desired structure with additional scripting. It does appear that domains and subtypes are preserved. I could not get SplitAttributes to create the features inside a dataset, but it might output to shape files. import arcpy
import os
gdb = r'C:\path\to\folder\COBW_Firealarm\COBW_Firealarm.gdb'
arcpy.env.workspace = gdb
out_folder = r'C:\path\to\folder\COBW_Firealarm' # geodatabases go here
fields = ['DRAWING_REFERENCE']
for fds in arcpy.ListDatasets('','Feature'):
print "Processing {}".format(fds)
sr = arcpy.Describe(fds).SpatialReference
for fc in arcpy.ListFeatureClasses('','',fds):
print "\t{}".format(fc) # use fc for name of target geodatabase
arcpy.CreateFileGDB_management(out_folder, '{}.gdb'.format(fc))
in_fc = os.path.join(gdb,fds,fc)
target = os.path.join(out_folder,'{}.gdb'.format(fc))
# print in_fc
# print target
# save split in file geodatabase with name of original feature class
arcpy.SplitByAttributes_analysis(in_fc, target, fields) Here's what the output looked like: Hope this helps.
... View more
02-05-2019
10:59 PM
|
1
|
3
|
4864
|
|
POST
|
And your version of ArcGIS? 10.5 or 10.6 or Pro ? Looks like Split By Attributes requires one of these versions.
... View more
02-05-2019
09:34 AM
|
0
|
0
|
4864
|
|
POST
|
When you say 'drawing number', are you referring to the DRAWING_REFERENCE field in your data? If so, I'm counting 10 different values. 111-111-AP
C0500-GS-0117-01-EL-FA-A0-03-001-001
C0500-GS-0117-01-EL-FA-A0-B1-001-001
C0500-GS-0117-04-EL-FA-A0-01-001-001
C0500-GS-0117-04-EL-FA-A0-02-001-001
C0500-GS-0117-04-EL-FA-A0-04-001-001
C0500-GS-0117-04-EL-FA-A0-05-001-001
C0500-GS-0117-04-EL-FA-A0-06-001-001
C0500-GS-0117-04-EL-FA-A0-B2-001-001
C0500-GS-0117-04-EL-FA-A0-GF-001-001
If this is not what you mean, could you explain 'drawing number'.
... View more
02-04-2019
08:19 PM
|
0
|
2
|
4864
|
|
POST
|
Great suggestions. Thank you. So far, the typical cause for an error in my testing was swapping the x and y coordinates.
... View more
02-03-2019
03:57 PM
|
0
|
0
|
3207
|
|
POST
|
Cursors or just printing (don't get x, y, lon, lat mixed up): import arcpy
latlon = arcpy.SpatialReference(4326) # wgs_1984
webmer = arcpy.SpatialReference(3857) # web mercator
nad = arcpy.SpatialReference(4267) # nad_1927
def convertXY(x,y,inSR,outSR):
try:
ptGeo = arcpy.PointGeometry(arcpy.Point(x,y),inSR).projectAs(outSR)
return (ptGeo.centroid.X, ptGeo.centroid.Y)
except:
print "\nERROR - cannot convert point\n x: {} y: {} inSR: {} outSR: {}".format(x,y,inSR.name,outSR.name)
return (0.0,0.0)
tbl = r"C:\Path\to\file.gdb\XYtable"
flds = [ 'x_4326','y_4326','x_3857','y_3857','x_4267','y_4267' ]
# using an update cursor, lon and lat fields are populated, calculate others
with arcpy.da.UpdateCursor(tbl, flds) as cursor:
for lon, lat, x_wm, y_wm, x_nad, y_nad in cursor:
x_wm, y_wm = convertXY(lon,lat,latlon,webmer)
x_nad, y_nad = convertXY(lon,lat,latlon,nad)
cursor.updateRow((lon, lat, x_wm, y_wm, x_nad, y_nad))
del cursor
# using an insert cursor -and/or- print
coords = [ # can come from text file, etc.
[175.5,-40.5],
[135.5,-30.5],
[90.5,-20.5],
[45.5,-10.5],
[0.0,0.0],
[-45.5,10.5],
[-90.5,20.5],
[-135.5,30.5],
[-175.5,40.5],
]
print "LON\tLAT\tX_WM\tY_WM\tX_NAD\tY_NAD"
cursor = arcpy.da.InsertCursor(tbl, flds)
for lon, lat in coords:
x_wm, y_wm = convertXY(lon,lat,latlon,webmer)
x_nad, y_nad = convertXY(lon,lat,latlon,nad)
print "{}\t{}\t{}\t{}\t{}\t{}".format(lon, lat, x_wm, y_wm, x_nad, y_nad)
cursor.insertRow((lon, lat, x_wm, y_wm, x_nad, y_nad))
del cursor
... View more
02-02-2019
10:34 PM
|
2
|
3
|
3207
|
|
POST
|
I've done some experimenting with the code you found for creating polygon fishnets. It adds some steps to clip the fishnet to the shape of the original polygons. I did not explore the group options in the code, so there may need to be some adjustments for these options to work correctly in the modified code. The basic changes involve tracking the object IDs of the starting polygons and clipping the fishnet boxes using the geometry intersect method. In my test, I had 3 polygons that I wanted to cover with a 20x20 grid and clip that to the shapes of the polygons. The results looked like: Here's the code starting around line 63 (following the set-up and functions in the code linked to in your question): # this would be line 63 following the functions and other set-up code
# ## read in_features geometry into dictionary for use with geometry intersect method later
origPolys = { r[0] : r[1] for r in arcpy.da.SearchCursor(in_features,['OID@','SHAPE@']) } # ## list comprehension
# Process the data
scratchName = arcpy.CreateScratchName("MBG","ByWidth","FeatureClass")
arcpy.MinimumBoundingGeometry_management(in_features,scratchName,"RECTANGLE_BY_WIDTH",group_option,group_fields,"MBG_FIELDS")
arcpy.DeleteField_management(scratchName,["MBG_Width","MBG_Length"]) # ## ["MBG_Width","MBG_Length","ORIG_FID"]) remove ORIG_ID from delete field list
arcpy.CreateFeatureclass_management(os.path.dirname(out_features),\
os.path.basename(out_features),"POLYGON",\
template=scratchName,\
spatial_reference=sR)
cur = arcpy.da.InsertCursor(out_features,["SHAPE@","MBG_Orientation","ORIG_FID"]) # ## ["SHAPE@","MBG_Orientation"]) add ORIG_ID to field list
if labels == "LABELS":
arcpy.CreateFeatureclass_management(os.path.dirname(out_features),\
os.path.basename(out_features)+"_label","POINT",\
spatial_reference=sR)
for row in arcpy.da.SearchCursor(scratchName,["OID@","SHAPE@","MBG_Orientation","ORIG_FID"]): # ## ["OID@","SHAPE@","MBG_Orientation"]): add ORIG_ID to field list
oid = row[0]
ofid = row[3] # ## original feature id ( in_features objectid )
arcpy.AddMessage("\nProcessing polygon " + str(oid))
mbgPoly = row[1]
orientation = float(row[2])
pivotPoint = mbgPoly.firstPoint
origin_coord = str(pivotPoint.X) + ' ' + str(pivotPoint.Y)
orthogonalPoly = RotatePolygon(mbgPoly,pivotPoint,orientation)
opposite_corner = str(orthogonalPoly.extent.XMax) + ' ' + str(orthogonalPoly.extent.YMax)
yCoord = str(pivotPoint.X) + ' ' + str(pivotPoint.Y + 1.0)
scratchName2 = arcpy.CreateScratchName("OrthogonalFishNet",str(oid),"FeatureClass")
arcpy.CreateFishnet_management(scratchName2,origin_coord,yCoord,cell_width,cell_height,number_rows,number_columns,labels=labels,template=orthogonalPoly.extent,geometry_type="POLYGON")
#arcpy.CreateFishnet_management(scratchName2,origin_coord,yCoord,cell_width,cell_height,number_rows,number_columns,opposite_corner,labels=labels,geometry_type="POLYGON")
# ## create a buffer from the original polygon which is in the dictionary
polyBuff = origPolys[ofid].buffer(0) # ## buffer distance either 0 or negative number in default spatial reference units
# arcpy.AddMessage("\npolyBuff = {}".format(polyBuff.JSON)) # ## for debugging
for cell in arcpy.da.SearchCursor(scratchName2,["OID@","SHAPE@"]):
arcpy.AddMessage("Processing fishnet cell..." + str(cell[0]))
outPoly = RotatePolygon(cell[1],pivotPoint,0-orientation)
# arcpy.AddMessage("\noutPoly = {}".format(outPoly.JSON)) # ## for debugging (json shows spatial reference as null)
# ## since outPoly has a null spatial reference, we need to specify it for the intersect method
newPoly = arcpy.FromWKT(outPoly.WKT,sR) # ## use WKT and add default spatial reference http://desktop.arcgis.com/en/arcmap/latest/analyze/arcpy-functions/fromwkt.htm
# arcpy.AddMessage("\nnewPoly = {}".format(newPoly.JSON)) # ## for debugging
# ## clip outPoly as required using geometry's intersect method
bufPoly = newPoly.intersect(polyBuff,4) # ## http://desktop.arcgis.com/en/arcmap/latest/analyze/arcpy-classes/geometry.htm (see intersect method)
# arcpy.AddMessage("\nbufPoly = {}".format(bufPoly.WKT)) # ## for debugging
# ## it is possible that the polygon falls outside the intersect clip
# ## json will show: bufPoly = {"rings":[],"spatialReference":{"wkid":102100,"latestWkid":3857}} and WKT will indicate EMPTY
if "EMPTY" not in bufPoly.WKT: # ## add only if geometry is not empty (null)
cur.insertRow([bufPoly,orientation,ofid])# ## [outPoly,orientation]) added field to save original polygon's objectid, see insertcursor around line 75
arcpy.Delete_management(scratchName2)
if labels == "LABELS":
del cur #Otherwise two InsertCursors in the same workspace requires arcpy.da.Editor
lcur = arcpy.da.InsertCursor(out_features+"_label",["SHAPE@"])
for label in arcpy.da.SearchCursor(scratchName2+"_label",["SHAPE@"]):
lcur.insertRow([arcpy.PointGeometry(RotatePoint(label[0].firstPoint,pivotPoint,0-orientation),sR)])
arcpy.Delete_management(scratchName2+"_label")
del lcur
cur = arcpy.da.InsertCursor(out_features,["SHAPE@","MBG_Orientation"])
arcpy.Delete_management(scratchName)
del cur
arcpy.AddMessage("\nDone") Hope this helps.
... View more
02-02-2019
02:20 PM
|
2
|
1
|
3756
|
|
POST
|
Thanks. From your code example, I used this in a project: if "EMPTY" not in polygonObject.WKT:
# do something
... View more
02-02-2019
01:24 PM
|
0
|
0
|
2287
|
|
POST
|
I believe for the add query or update (OBJECTID field required) query, the features section is all that is needed. You shouldn't need "fields" or the other sections. {
"features": [{
"attributes": {
"RECORD_ID": 1,
"DATE_TIME": 1546359300000,
"SPEED": 1,
"HEADING": 12
},
"geometry": {
"paths": [
[
[-5871083.00245447, 6028331.87508284],
[-5871713.60793782, 6027605.72331413]
]
]
}
}]
} I did notice that the spatial reference of the line layer returned is 3857 - web mercator (see below). From your first json example, it looks like you are trying to use 4326 - lon/lat. You may need to convert your coordinates before the update query with a projectAs. "spatialReference": {
"wkid": 102100,
"latestWkid": 3857
}
... View more
01-31-2019
12:29 PM
|
1
|
0
|
4725
|
|
POST
|
I am assuming you wish to remove the part of the fishnet that is outside the red lines. The section of code you highlighted is a possibility for adding the code for clipping. The issue I see is that a link to the original polygon is lost in the process by the DeleteField_management "ORIG_FID" line in the code. There are several places that the ORIG_FID would need to be referenced in some of the lines that follow. The reference to mbgPoly is to the minimum bounding box polygon, so it wouldn't do any clipping to the fishnet. The use of multiple cursors in that section of code would also be problematic as noted in the nearby comment. It might be possible to read the required geometry into a dictionary that could be referenced. And, I'm sure there are other solutions, too.
... View more
01-31-2019
11:03 AM
|
0
|
1
|
3756
|
|
POST
|
Also, checking json with jsonlint.com will also help locate issues. So, perhaps something like: {
"features": {
"geometry": {
"paths": [
[
[0, 0],
[1, 1]
]
],
"spatialReference": {
"wkid": 4326
}
},
"attributes": {
"RECORD_ID": 1,
"DEVICE_ID": 1,
"DEVICE_NAME": "NAME",
"DATE_TIME": 123,
"SPEED": 1,
"HEADING": 1
}
}
}
... View more
01-31-2019
10:29 AM
|
0
|
0
|
4725
|
|
POST
|
When you request a line feature, what does the returned json look like (specifically, the geometry portion)?
... View more
01-31-2019
09:41 AM
|
0
|
0
|
4725
|
|
POST
|
And the page referenced also says: A Lot Has Changed Since SQL-92 And so it has.
... View more
01-29-2019
12:15 PM
|
1
|
0
|
825
|
|
POST
|
At one time, I would swear that I used "LEFT" with a file geodatabase. But it wasn't working today ... which I attributed to just being a Monday!
... View more
01-28-2019
12:24 PM
|
0
|
1
|
7643
|
|
POST
|
I think since you are using a file geodatabase you might need to use something like (you'll want to make sure both sides are a string of 3 characters): SUBSTRING("OWNERSHIP_NUMBER" FROM 1 FOR 3) = SUBSTRING("Layer" FROM 1 FOR 3) (This from an old thread: Definition query SQL won't accept Left() Function) And according to SQL reference for query expressions used in ArcGIS All SQL used by the file geodatabase is based on the SQL-92 standard.
... View more
01-28-2019
11:29 AM
|
1
|
8
|
7643
|
| Title | Kudos | Posted |
|---|---|---|
| 1 | 10-27-2016 02:23 PM | |
| 1 | 09-09-2017 08:27 PM | |
| 2 | 08-20-2020 06:15 PM | |
| 1 | 10-21-2021 09:15 PM | |
| 1 | 07-19-2018 12:33 PM |
| Online Status |
Offline
|
| Date Last Visited |
02-12-2026
07:13 PM
|