Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of arcpy in python

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article shares with you the content of the sample analysis of arcpy in python. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Demand

There are multiple file geographic databases (gdb), in which there are multiple area feature class layers, each of which cannot overlap itself or overlap with other layers. Therefore, you need to do a topology check for each file geographic database (gdb).

Train of thought

1. Create a new gdb under the output folder with the same name as the input folder.

two。 Create a new feature class dataset within gdb.

3. Create a topology within the dataset.

4. Add feature classes to the topology.

5. Add a topology rule.

6. Topology verification.

7. Export topology error.

Put the gdb that needs the topology into the same folder, concatenate it with python code and use the loop to traverse the gdb to do the batch topology.

Code #-*-coding: gbk-*-import osimport arcpyimport sysreload (sys) sys.setdefaultencoding ('utf-8') arcpy.AddMessage (sys.getdefaultencoding ()) arcpy.env.XYResolution = "0.0001 Meters" # set XY resolution arcpy.env.XYTolerance = "0.001 Meters" # set XY tolerance def checkTopology (in_path Out_path): cout = 0 fail = 0 warning = 0 fail_list = [] warning_list = [] arcpy.env.workspace = in_path workspaces = arcpy.ListWorkspaces ("*" "ALL") for workspace in workspaces: arcpy.AddMessage ("=" * 60) cout + = 1 arcpy.AddMessage ("(" + str (cout) + ")" + os.path.basename (workspace)) arcpy.AddMessage ("") try: # set each gdb to the workspace arcpy.env.workspace = workspace fc_ List = arcpy.ListFeatureClasses () # list the feature class layers within gdb in_fc_path_list = [] for fc in fc_list: in_fc_path_list.append (os.path.join (workspace) Fc)) # New gdb gdb_name1 = os.path.basename (workspace) result_gdb = os.path.join (out_path, gdb_name1) if not arcpy.Exists (result_gdb): arcpy.AddMessage ("New" + result_gdb + "...") under the output path out_path Arcpy.CreateFileGDB_management (out_path, gdb_name1) else: arcpy.AddWarning (result_gdb + "already exists!") Warning_list.append (result_gdb) warning + = 1 continue arcpy.AddMessage ("create a new feature dataset within" + result_gdb + ") Dataset_name = "dataset" dataset_path = os.path.join (result_gdb,dataset_name) # refers to the feature class or feature data set that contains the spatial reference to be applied, # uses the coordinates of in_fc_path_list [0] as the coordinates of the feature data set So all feature classes in gdb should be unified coordinates arcpy.CreateFeatureDataset_management (result_gdb, dataset_name, in_fc_path_list [0]) # Import feature classes into dataset arcpy.AddMessage ("Import feature class layers to" + dataset_path + "). Arcpy.FeatureClassToGeodatabase_conversion (in_fc_path_list,dataset_path) # create a topology arcpy.AddMessage within dataset data ("create topology.") Topology_name = "topology" topology_path = os.path.join (dataset_path, topology_name) arcpy.CreateTopology_management (dataset_path, topology_name) # add the feature classes imported into dataset to the topology arcpy.AddMessage ("add feature classes to the topology.") Dataset_fc_path_lsit = [] # absolute path list of feature classes in the dataset for ifc_name in fc_list: # absolute path of the feature class in the spliced dataset dataset_fc_path = os.path.join (dataset_path Ifc_name) dataset_fc_path_lsit.append (dataset_fc_path) arcpy.AddFeatureClassToTopology_management (topology_path, dataset_fc_path, "1", "1") arcpy.AddMessage ("add topology rules...") For i in range (len (dataset_fc_path_lsit)): fc_path2 = dataset_fc_path_ [I] # Rule 1: cannot overlap arcpy.AddRuleToTopology_management (topology_path, "Must Not Overlap (Area)", fc_path2, ") for j in range (I + 1) Len (dataset_fc_path_lsit): fc_path3 = dataset_fc_path_ LSIT [j] # Rule 2: do not overlap with other feature classes arcpy.AddRuleToTopology_management (topology_path, "Must Not Overlap With (Area-Area)", fc_path2, ", fc_path3 ") arcpy.AddMessage (" Topology Verification... ") Arcpy.ValidateTopology_management (topology_path, "Full_Extent") arcpy.AddMessage ("Export topology error...") Arcpy.ExportTopologyErrors_management (topology_path, dataset_path, "topoError") except Exception ase: arcpy.AddError (e.message) arcpy.AddError ("" + os.path.basename (workspace) + "fail!") Fail_list.append (os.path.basename (workspace)) fail + = 1 arcpy.AddMessage ('+'* 60) arcpy.AddMessage (u "success:" + str (cout-fail-warning) + u "!") If warning > 0: arcpy.AddWarning (u "warning:" + str (warning) + u "! As follows: ") arcpy.AddWarning (" # "+'* * 20) for fff in warning_list: arcpy.AddWarning (" + fff) arcpy.AddWarning ("#" +'* * 20) if fail > 0: arcpy.AddError (u "failed:" + str (fail) + u "! As follows: ") arcpy.AddError (" # "+'* * 20) for ff in fail_list: arcpy.AddError (" + ff) arcpy.AddError ("#" +'* 20) arcpy.AddMessage ('+ 60) if _ name__ = = "_ main__": in_path_globle = arcpy.GetParameterAsText (0) Out_path_globe = arcpy.GetParameterAsText (1) checkTopology (in_path_globle Out_path_globe) effect drawing

Thank you for reading! This is the end of this article on "sample Analysis of arcpy in python". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report