|
POST
|
With SQL, I think the "proper" way to do this would be with regular expressions (regex). Not sure if you need SQL Server or Oracle syntax but you can go on the regex adventure yourself. An uglier/messier way to do this would be to just have three LIKE statements; one for each possible scenario the search string could exist. where intx_field like '1,%' -- Beginning
or intx_field like '%,1,%' -- Middle
or intx_field like '%,1' -- End
or intx_field = '1' -- Single Editied to include where clause for single value; thanks Richard Fairhurst
... View more
06-16-2016
02:30 PM
|
1
|
5
|
2197
|
|
POST
|
Looks like you're basically using example 2 from the Esri docs. How are you running this script? You might need to run it inside ArcMap. Also, there's a nice way of formatting when Posting Code blocks in Esri GeoNet
... View more
06-13-2016
10:23 AM
|
0
|
3
|
1444
|
|
POST
|
Paul Hacker says: Now if I can just find some way to use the same CreatePersonalGDB_management/CreateTable_management/AddField_management to make the database and then in the tables slip in a SHAPE that is a polygon, that would do what I need to be done. Joshua Bixby replied: ...the proper method to create a spatially-enabled table (one with a SHAPE field) is to use the Create Feature Class tool. The tool does multiple things for you, one it creates a table, and two it adds the spatial field. The result is a new table with a SHAPE field already defined for you. If your goal is to create a SHAPE field in a Access table not using Esri tools, the short answer is don't. There's really nothing more to be said. Not sure why you're so stuck on trying to reinvent the wheel when it comes to making a feature class.
... View more
06-08-2016
08:16 AM
|
1
|
2
|
1303
|
|
POST
|
Here's what I used to save the JSON result from a map service query to a feature class. def main():
import arcpy
import urllib
svc_lyr_url = "https://mydomain.com/arcgis/rest/services/MyServiceName/MapServer/1"
where_clause = "objectid=1" ## Required
field_names = "" ## Optional "FIELD1,FIELD2,FIELD3"
# Test availability of service
try:
svc_lyr_response = urllib.urlopen(svc_lyr_url)
print svc_lyr_response
if svc_lyr_response.getcode() == 200: ## The request has succeeded
# Build and format query url
query_url = "{}/query?where={}&outFields={}&returnGeometry=true&f=json".format(
svc_lyr_url,
where_clause,
field_names,
)
try:
query_response = urllib.urlopen(query_url)
print query_response
if query_response.getcode() == 200: ## The request has succeeded
print "http code {} from {}".format(svc_lyr_response.getcode(), query_response.geturl())
# Load JSON data from query and copy to feature class
fs = arcpy.FeatureSet()
fs.load(query_url)
arcpy.env.overwriteOutput = True ## Optional
arcpy.CopyFeatures_management(fs, r"C:\temp\TEMP.gdb\temp1")
print arcpy.GetMessages()
else:
response_msg = "http code {} from {}".format(
query_response.getcode(),
query_response.geturl()
)
raise Exception(response_msg)
finally:
query_response.close()
else:
response_msg = "http code {} from {}".format(
svc_lyr_response.getcode(),
svc_lyr_response.geturl()
)
raise Exception(response_msg)
finally:
svc_lyr_response.close()
if __name__ == '__main__':
main()
... View more
06-06-2016
09:38 AM
|
4
|
4
|
2879
|
|
POST
|
You can actually go one step further and use the DISTINCT SQL prefix so you don't have to query everything, just do do your own distinct with a set in that generator expression. See the code sample 5b and 6 for reference on using SQL prefix and postfix. My approach would be something like this import arcpy
import os
def main():
source_table = os.path.join(workspace, outname)
gis_fields_in = ["ADDR_NUM", "FULL_ST_NAME"]
distinct_names = [
row[0] for row in
arcpy.da.SearchCursor(
source_table, ## in_table
"Name", ## field_names
sql_clause=(
'DISTINCT', ## SQL prefix
None ## SQL postfix
)
)
]
for name in distinct_names:
out_table = os.path.join(workspace, "{}_Name_{}".format(outname, name))
where_clause = "Name = '{}'".format(name)
try:
arcpy.MakeTableView_management(source_table, "tbl_view", where_clause)
arcpy.CopyRows_management("tbl_view", out_table)
finally:
arcpy.Delete_management("tbl_view")
if __name__ == '__main__':
main()
... View more
06-03-2016
01:15 PM
|
2
|
0
|
1382
|
|
POST
|
I didn't know arcpy had its own time. Put on your parachute pants and get ready for Esri Time! a parady of Hammer Time!
... View more
06-03-2016
10:36 AM
|
0
|
4
|
8069
|
|
POST
|
You need to use the workspace (with the name of the mdb), not just the folder location like you're doing with out_folder_path. Same goes for adding the fields. You either need to define an arcpy.env.workspace() or use the full path to the geodatabase and table. But better yet, build your paths better with raw strings and the os.path module. Filenames and file paths in Python Also, looks like you're trying to add a shape field. Is this supposed to be a feature class? If it's a feature class (a table with with a shape field), you should also be defining a spatial reference. Finally, is there a particular reason you're using a personal geodatabase instead of a file geodatabase, which more robust and future-proof in the Esri world? Types of geodatabases—ArcGIS Help | ArcGIS for Desktop
... View more
06-03-2016
09:43 AM
|
1
|
1
|
3782
|
|
POST
|
You want to print every row in every CSV to the Python interpreter? Doesn't seem useful to me. Maybe start smaller and get each individual piece working. There is no obvious reason why this should not be working for you. Start by looping over your folder of MXDs and print the file name and layer names. In a new script, see if you can open a search cursor on one feature class and write the rows to CSV. This would have nothing to do with a map document. Finally, once you have those working, put those two bits together to export the CSV for every layer in every MXD.
... View more
05-31-2016
02:19 PM
|
0
|
1
|
2046
|
|
POST
|
Ultimately, the print out would have the mxd name and feature class name. I'm not quite sure what you mean by "the print out."
... View more
05-31-2016
01:15 PM
|
0
|
3
|
2046
|
|
POST
|
Using the layer.dataSource as your input for the search cursor like Darren mentioned should do the trick. Here's what the last part should look like. for layer in layers:
if layer.isFeatureLayer:
lyr_source = layer.dataSource
lyr_name = layer.name.encode("utf8", "replace")
csv_name = "{}.csv".format(lyr_name)
with open(csv_name, "w") as csvfile:
csvwriter = csv.writer(csvfile, delimiter=",", lineterminator="\n")
with arcpy.da.SearchCursor(lyr_source, "*") as s_cursor:
for row in s_cursor:
csvwriter.writerow(row) Keep these things in mind with how you've structured your code so far: What happens if the same layer name exists in more than one mxd? Do you want to traverse down into subfolders looking for map docs? Do you want to check for other data frames or group layers? Do you want the field names written to the csv? (I recommend writing field names)
... View more
05-31-2016
10:07 AM
|
1
|
5
|
2046
|
|
POST
|
What exactly are you trying to do? It looks like you are exporting every feature class to a CSV from every MXD in a folder.
... View more
05-27-2016
03:44 PM
|
0
|
1
|
2046
|
|
POST
|
I don't think this is a valid path to a feature class 'C:\MY_TEMPLATES\CW\CW_ENV_FMSE_EDIT.mxdDatabase Connections\CW_ENV_uCW_ENV_vCW_ENV.sde\PW.EnvironmentalCompliance\PW.ServiceEstablishments' You need to go directly to the sde connection file, then to your feature class. Don't use the MXD path first. The full path to a feature class should look like 'Database Connections\CW_ENV_uCW_ENV_vCW_ENV.sde\PW.EnvironmentalCompliance\PW.ServiceEstablishments' I think this is what Darren is getting at. Put some print statements in there to write what's in your variables so you know what's happening and where it's failing.
... View more
05-27-2016
01:22 PM
|
1
|
9
|
2046
|
|
POST
|
First, there's a nice way of posting code in GeoNet. As for your error, first make sure you're building your paths correctly and that the feature class you're after actually exists. Also, check that last line in the code. You want to write each row to the csv, not fields.
... View more
05-27-2016
08:47 AM
|
1
|
0
|
566
|
|
POST
|
You have to give at least one field to the search cursor to get data for. You can leave out writing the field names in the CSV by removing line 16 from my code example above. csvwriter.writerow(fields)
... View more
05-27-2016
08:27 AM
|
0
|
22
|
8740
|
|
POST
|
Try writerows() instead of writerow(). That works if you have all of your data in one Python iterable like a list of lists or tuples (no need to use it with a for loop). I use it when writing out from a SQL query. It also looks like you are missing a line to write your field names. And like Darren mentioned, your just using a path as your data, you need something to actually read it. Here's the code I use for writing a feature class table out to a CSV line by line. import arcpy
import os
import csv
# Environment variables
workingDir = r"C:\temp"
workingGDB = os.path.join(workingDir, "MyGeodatabase.gdb")
inputTable = os.path.join(workingGDB, "MyInputTable")
outputCSV = os.path.join(workingDir, "MyOutput.csv")
# Create CSV
with open(outputCSV, "w") as csvfile:
csvwriter = csv.writer(csvfile, delimiter=',', lineterminator='\n')
## Write field name header line
fields = ['FirstField','NextField','AndThirdExample']
csvwriter.writerow(fields)
## Write data rows
with arcpy.da.SearchCursor(inputTable, fields) as s_cursor:
for row in s_cursor:
csvwriter.writerow(row)
... View more
05-26-2016
12:21 PM
|
1
|
0
|
8740
|
| Title | Kudos | Posted |
|---|---|---|
| 1 | 4 weeks ago | |
| 1 | 10-23-2025 03:53 PM | |
| 1 | 04-28-2026 07:25 AM | |
| 1 | 03-19-2026 08:59 AM | |
| 1 | 02-12-2026 01:37 PM |