【444】Data Analysis (shp, arcpy)
ABS suburbs data of AUS
1. Dissolve
Merge polygons with the same attribute of "SA2_NAME16".
1 2 3 4 5 6 7 | >>> import arcpy >>> mxd = arcpy.mapping.MapDocument( "CURRENT" ) >>> df = arcpy.mapping.ListDataFrames(mxd)[ 0 ] >>> lyrs = arcpy.mapping.ListLayers(df) >>> for lyr in lyrs: ... arcpy.Dissolve_management(lyr, "Dissolve_" + lyr.name, 'SA2_NAME16' , '#' , 'MULTI_PART' , 'DISSOLVE_LINES' ) ... |
2. Add Centroid XY
After using the "Add Geometry Attributes" tool, we should close shp files and add them again and will see the results. (Sometimes it can show directly, WTF!!!)
1 2 3 4 | ... >>> for lyr in lyrs: ... arcpy.AddGeometryAttributes_management(lyr, "CENTROID" ) ... |
3. Add a state field
Before merging those polygons, we should point a specific field storing state info.
Before doing this, atrribute window should be closed, or it won't work.
1 2 3 4 5 6 7 8 9 10 11 12 13 | ... >>> for i in range ( 1 , 9 ): ... arcpy.AddField_management(lyrs[i], "State" , "TEXT" ) ... >>> # file name like "Suburbs_MB_2016_NSW" >>> # we want to get "NSW" >>> for i in range ( 1 , 9 ): ... cursor = arcpy.UpdateCursor(lyrs[i]) ... fn = lyrs[i].name ... for row in cursor: ... row.setValue( "State" , fn[fn.rfind( "_" ) + 1 :]) ... cursor.updateRow(row) ... |
4. Merge the whole polygons into one
1 2 3 4 | >>> mxd = arcpy.mapping.MapDocument( "CURRENT" ) >>> df = arcpy.mapping.ListDataFrames(mxd)[ 0 ] >>> lyrs = arcpy.mapping.ListLayers(df) >>> arcpy.Merge_management(lyrs, "Suburbs_MB_2016_AUS" ) |
5. Export table to csv file
Tool: Export Feature Attribute to ASCII
ref: Export an attribute table to .txt using arcpy.
1 | >>> arcpy.ExportXYv_stats( 'Suburbs_MB_2016_AUS' , 'SA2_NAME16;CENTROID_X;CENTROID_Y;State;Shape_Area' , 'COMMA' , r 'D:\Twitter Data\Data\test\2.csv' , 'ADD_FIELD_NAMES' ) |
6. Get specific columns
Based on pandas lib.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | >>> df = pd.read_csv(r "D:\Twitter Data\Data\test\2.csv" ) >>> df.head() XCoord YCoord ... STATE SHAPE_AREA 0 117.899601 - 35.008360 ... WA 0.003012 1 118.207172 - 34.718972 ... WA 0.394533 2 115.865812 - 31.834866 ... WA 0.000638 3 115.677976 - 31.600241 ... WA 0.003104 4 115.836085 - 32.019166 ... WA 0.000518 [ 5 rows x 7 columns] >>> df.columns Index([ 'XCoord' , 'YCoord' , 'SA2_NAME16' , 'CENTROID_X' , 'CENTROID_Y' , 'STATE' , 'SHAPE_AREA' ], dtype = 'object' ) >>> df1 = df[[ 'SA2_NAME16' , 'CENTROID_X' , 'CENTROID_Y' , 'STATE' , 'SHAPE_AREA' ]] >>> df1.columns Index([ 'SA2_NAME16' , 'CENTROID_X' , 'CENTROID_Y' , 'STATE' , 'SHAPE_AREA' ], dtype = 'object' ) >>> df1.head() SA2_NAME16 CENTROID_X ... STATE SHAPE_AREA 0 Albany 117.899601 ... WA 0.003012 1 Albany Region 118.207172 ... WA 0.394533 2 Alexander Heights - Koondoola 115.865812 ... WA 0.000638 3 Alkimos - Eglinton 115.677976 ... WA 0.003104 4 Applecross - Ardross 115.836085 ... WA 0.000518 [ 5 rows x 5 columns] >>> df1.to_csv(r "D:\Twitter Data\Data\test\Suburbs_AUS.csv" , index = False ) |
7.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)