POST
|
Dear all, Thanks for your answers. In the end I've built a python solution to export adjacency matrix and node list of a network to text files. It requires the installation of networkX and shapely python packages. Your network has to be a shapefile, and the script was not tested on big graphs. I have uploaded a zip file containing script, data sample and results hereHope this helps and feel free to comment ! Best regards, Theo # Extract adjacency matrix and nodelist from Shapefile with python networkX
#Packages import
import os
import networkx as nx
import shapely
import numpy as np
import re
# Setting the working directory (the network .shp file has to be in this path)
path = r'Q:\gis\NetworkSample'
# Now change the directory
os.chdir( path )
# Check current working directory.
retval = os.getcwd()
print "Directory changed successfully %s" % retval
# Name of the network shapefile
shpFileName = 'SampleNetworkData.shp'
shpFileNameStr=shpFileName.replace(".","_")
# Adjacency matrix export name
AdjMatName="Adj_matrix_dist_"+shpFileNameStr+".txt"
# node list export name
NodeListName="Nodes_of_"+shpFileNameStr+".txt"
#Reading the shp and creating the corresponding graph G
G=nx.read_shp(shpFileName)
#Converting G to an unoriented graph
G=G.to_undirected()
# Print the number of nodes and edges
n=G.number_of_nodes()
e=G.number_of_edges()
print str(G.number_of_nodes()) +" nodes" + " and " +str(G.number_of_edges()) + " edges"
#Change nodes labels by an integer that range from 0 --> n and save the old labels (XY coordinates).
G=nx.convert_node_labels_to_integers(G,label_attribute='XYcoordinates')
# Adjacency Matrix
# Computing adjacency matrix using length as weight
adjMatrix=nx.adjacency_matrix(G,nodelist=None,weight='Shape_Leng')
# Converting CSR matrix format to dense format
DenseAdjMatrix=adjMatrix.todense()
# Exporting adjacency matrix as .txt file
np.savetxt("dense_adjacency_matrix_SampleNetworkData.txt", DenseAdjMatrix, delimiter=',', newline="\n", fmt='%d')
# Nodes attributes
# Exporting the nodes key and coordinates has a file ("nodelist.txt")
with open('nodeslist.txt','w') as file:
for node in G.nodes_iter(data=True):
#Identify the node key (integer) and X,Y coordinates (float) and return them in the list 'towrite'
towrite=re.findall(r"[-+]?\d*\.\d+|[-+]?\d+", str(node))
#Setting file format (node key,X,Y) and export
towrite= ','.join(map(str, towrite))
print>>file, towrite
#End of script
... View more
07-10-2015
02:45 AM
|
1
|
0
|
1895
|
DOC
|
Export adjacency matrix and nodes list of a network (shapefile) to text files. It requires the following python packages: networkx, shapely, numpy, re This archive contains the python script, sample data and results.
... View more
07-10-2015
02:30 AM
|
0
|
0
|
3834
|
POST
|
Dear all, I've been using modelbuilder to create a basic model of an OD Cost Matrix analysis. I exported it to a python script to create script tool (code below). Unfortunately I keep getting the following error: "ERROR 000732: Input network analysis layer: Dataset C:\GIS\databases\fullnetwork.gdb\fullnetwork\fullnetwork_ND does not exist or is not supported." I've also tried "C:/GIS/databases/fullnetwork.gdb/fullnetwork/fullnetwork_ND" but i got the same error. The model has 4 required parameters (script tool parameters): - Network / Network Dataset / Input - Origins / Feature Class / Input - Destinations / Feature Class / Input - NA_Output / Feature Class / Output I really don't understand what I am doing wrong... any advice welcome 🙂 Best regards, Theo [PHP] # --------------------------------------------------------------------------- # OD_COST_PYTHON_Brut3.py # Created on: 2012-05-21 13:42:11.00000 # (generated by ArcGIS/ModelBuilder) # Usage: OD_COST_PYTHON_Brut3 <fullnetwork_ND__2_> <Origin_feature_points> <Destination_feature_points> <line_feature_output> # Description: # --------------------------------------------------------------------------- # Set the necessary product code # import arcinfo # Import arcpy module import arcpy # Check out any necessary licenses arcpy.CheckOutExtension("Network") # Script arguments fullnetwork_ND__2_ = arcpy.GetParameterAsText(0) if fullnetwork_ND__2_ == '#' or not fullnetwork_ND__2_: fullnetwork_ND__2_ = "fullnetwork_ND" # provide a default value if unspecified Origin_feature_points = arcpy.GetParameterAsText(1) if Origin_feature_points == '#' or not Origin_feature_points: Origin_feature_points = "oioiuiuo_origins" # provide a default value if unspecified Destination_feature_points = arcpy.GetParameterAsText(2) if Destination_feature_points == '#' or not Destination_feature_points: Destination_feature_points = "oioiuiuo_origins" # provide a default value if unspecified line_feature_output = arcpy.GetParameterAsText(3) if line_feature_output == '#' or not line_feature_output: line_feature_output = "D:\\pythonproject\\premierpas\\databases\\scratch.gdb\\odout" # provide a default value if unspecified # Local variables: Destinations = "OD Cost Matrix\\Destinations" OD_Cost_Matrix__3_ = Origin_feature_points OD_Cost_Matrix__5_ = OD_Cost_Matrix__3_ OD_Cost_Matrix__2_ = OD_Cost_Matrix__5_ OD_Cost_Matrix__6_ = OD_Cost_Matrix__2_ Lines = OD_Cost_Matrix__6_ Lines__3_ = Lines Lines__4_ = Lines__3_ Solve_succeeded = OD_Cost_Matrix__2_ Origins = "OD Cost Matrix\\Origins" OD_Cost_Matrix = fullnetwork_ND__2_ OD_Cost_Matrix__4_ = OD_Cost_Matrix # Process: Make OD Cost Matrix Layer arcpy.MakeODCostMatrixLayer_na(fullnetwork_ND__2_, "OD Cost Matrix", "Length", "", "", "", "ALLOW_UTURNS", "", "NO_HIERARCHY", "", "STRAIGHT_LINES") # Process: Add Field to Analysis Layer arcpy.AddFieldToAnalysisLayer_na(OD_Cost_Matrix, "Origins", "OriginAddressID", "LONG", "", "", "", "", "NULLABLE") # Process: Add Locations arcpy.AddLocations_na(OD_Cost_Matrix__4_, "Origins", Origin_feature_points, "Name OBJECTID #;TargetDestinationCount # #;CurbApproach # 0;Cutoff_Length # #;OriginAddressID OBJECTID #;SourceID SourceID #;SourceOID SourceOID #;PosAlong PosAlong #;SideOfEdge SideOfEdge #", "5000 Meters", "", "NetworkFull SHAPE;fullnetwork_ND_Junctions NONE", "MATCH_TO_CLOSEST", "APPEND", "NO_SNAP", "5 Meters", "INCLUDE", "NetworkFull #;fullnetwork_ND_Junctions #") # Process: Add Field to Analysis Layer (2) arcpy.AddFieldToAnalysisLayer_na(OD_Cost_Matrix__3_, "Destinations", "DestinationAddressID", "LONG", "", "", "", "", "NULLABLE") # Process: Add Locations (2) arcpy.AddLocations_na(OD_Cost_Matrix__5_, "Destinations", Destination_feature_points, "Name OBJECTID #;CurbApproach # 0;DestinationAddressID OBJECTID #;SourceID SourceID #;SourceOID SourceOID #;PosAlong PosAlong #;SideOfEdge SideOfEdge #", "5000 Meters", "", "NetworkFull SHAPE;fullnetwork_ND_Junctions NONE", "MATCH_TO_CLOSEST", "CLEAR", "NO_SNAP", "5 Meters", "INCLUDE", "NetworkFull #;fullnetwork_ND_Junctions #") # Process: Solve arcpy.Solve_na(OD_Cost_Matrix__2_, "SKIP", "CONTINUE") # Process: Select Data arcpy.SelectData_management(OD_Cost_Matrix__6_, "Lines") # Process: Join Field arcpy.JoinField_management(Lines, "DestinationID", Destinations, "OBJECTID", "DestinationAddressID") # Process: Join Field (2) arcpy.JoinField_management(Lines__3_, "OriginID", Origins, "OBJECTID", "OriginAddressID") # Process: Copy Features arcpy.CopyFeatures_management(Lines__4_, line_feature_output, "", "0", "0", "0") [/PHP]
... View more
05-21-2012
04:31 AM
|
0
|
1
|
704
|
POST
|
Hello Elina ! You can easily compute all the distances between your points and the city centre with the Closest Facility tool (Network Analyst). You can load the city centre as "Facilities" and all the other points as "Incidents". Just have a look at the ArcGIS help here: http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//00470000004n000000 Also have a look at the OD Cost Matrix help. As far as I know you can't use polygon, maybe you could just compute its centroid and then just load it as a "Facilities". Best regards, Theo
... View more
05-15-2012
05:45 AM
|
0
|
0
|
211
|
POST
|
Sorry, this question has already been asked: http://forums.arcgis.com/threads/55350-Economizing-OD-Cost-Matrix-when-A-gt-B-B-gt-A Theo Hello all, For research purposes, I am currently solving OD Cost Matrix to compute shortest paths distances for about 10'000 origins and destinations (origins = destinations). I am using a undirected network, so the distance from A to B equal distance from B to A (symmetric distances). Therefore, do anyone has an idea on how to customize the OD Cost Matrix tool so that it only computes A to B and not B to A ? This would halve the size of the database and the computing time. Any idea ? Best regards, Theo
... View more
05-01-2012
09:14 AM
|
0
|
0
|
257
|
POST
|
Hi again, You can use "Add Field (Data Management)" and "Calculate Field (Data Management)" tools inside Modelbuilder. Note that if you want to get values from another table you will first have to join the two tables ("Join Field" or "Add Join"). I am off for tonight, Best regards, Theo
... View more
05-01-2012
09:11 AM
|
0
|
0
|
766
|
POST
|
Hello all, For research purposes, I am currently solving OD Cost Matrix to compute shortest paths distances for about 10'000 origins and destinations (origins = destinations). I am using a undirected network, so the distance from A to B equal distance from B to A (symmetric distances). Therefore, do anyone has an idea on how to customize the OD Cost Matrix tool so that it only computes A to B and not B to A ? This would halve the size of the database and the computing time. Any idea ? Best regards, Theo
... View more
05-01-2012
09:03 AM
|
0
|
2
|
401
|
POST
|
As far as I know, the output table is a dBASE or geodatabase table. You should be able to browse to an ArcGIS database (.gdb) and give the name of your choice to the output table. In this case you don't have to provide an extension to the file. For example: D:\GIS\mydatabase.gdb\destination_summary_table Cheers, Theo
... View more
05-01-2012
08:49 AM
|
0
|
0
|
766
|
POST
|
Hello Nathan, As far as I understand your problem, you should look to the tool named "Summary Statistics". In your case you can use your destination field ("The lines all have a field that contains ONLY the destination") as the "Case field". Then just select the others fields in "Statistics Field(s)". You will then be able to calculate their SUM (or MEAN,..) for each "Destination Field value (Dest1, Dest2,...)" and export the results to a new table. Link to "Summary Statistics": http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//00080000001z000000 Hope this helps :confused: Cheers, Theo
... View more
05-01-2012
08:06 AM
|
0
|
0
|
766
|
POST
|
Dear Jay, Thanks a lot for your answer that helped me enough to succeed 😄 I've attached 2 screenshots of my OD Cost Matrix layer in Modelbuilder thinking that they might be helpfull to somebody. Best regards, Theo
... View more
11-01-2011
06:18 AM
|
0
|
0
|
620
|
POST
|
Dear all, I have a basic street network. For research purpose, I need to access and export (.dbf, .txt,...) the "connectivity table" of my network exactly as shown in the joined screenshot: Junction / Adjacent junction and edge elements I'm using ArcGIS 10. Any advice welcome.. Best regards, Theo ps. The screeenshot as been taken on http://edndoc.esri.com/arcobjects/8.3/TechnicalDocuments/Network/ArcGISNetworkModel/ArcGISNetwork.htm
... View more
10-24-2011
01:36 AM
|
0
|
5
|
9712
|
POST
|
Dear all, I am running several OD cost matrix analysis. Working with the "Lines" table output, I am wondering if it???s possible to add (or compute) a field during the solve process. By default the field names of the Lines table are: ObjectID/Shape/Name/OriginID/DestinationID/DestinationRank/Total_Length For example is it possible to have a Name2 field that would work exactly the same as the Name field ? Or another field like OriginID that contains a values from the Origins table ? Here???s my situation : Origins and destinations are addresses (point features) of my city and have the same table structure : ObjectID/Shape/StreetName/StreetNum/AddressID/NeighbourID « AddressID » is a unique address identifier. « NeighbourID » identify the zone (neighbourhood) in which the address point is located. My goal is to get the following « Lines » table after solving the OD cost matrix : ObjectID/Shape/Name/OriginID/DestinationID/DestinationRank/Total_Length/OriginNeighbourID/DestinationNeighbourID/Name2 (see screenshot attached) The only thing I found is the "add field to analysis layer" tool, but as far as I know it only adds field to Origins, Destinations, Barriers prior to the analysis. Any idea ? Best regards, Theo
... View more
09-21-2011
06:38 AM
|
0
|
4
|
3541
|
POST
|
Dear all, I am currently trying to compute a lot of shortest paths between about 10'000 locations with network analyst (OD Cost Matrix). Because of long calculation time, I�??ve decided to run the analysis on multiple smaller dataset (~1'000 locations). Now let�??s say I have the following tables : Table 1 field: Name_ID Table 2 fields: Name_ID / distance Table 3 fields : Name_ID / distance Table 1 has been generated by a python script and contains all the possible « Name_ID ». Based on the « Name_ID » field, I want to add (or update) the field « distance » (Table 2 and Table 3) to Table 1. If I use a join to do this I got the following table : Table_1.Name_ID / Table_2.Name_ID / Table_2.distance / Table_3.Name_ID / Table_3.distance / And I want to have : Name_ID / distance Can someone give me an advice on how to do this ? Can I do a SQL left join ? Is it possible to update an existing table with the "make query table" ? Thanks in advance, Regards, Theo ps. I'am on ArcGIS 10
... View more
09-09-2011
05:33 AM
|
0
|
1
|
250
|
Title | Kudos | Posted |
---|---|---|
1 | 07-10-2015 02:45 AM |
Online Status |
Offline
|
Date Last Visited |
04-05-2022
06:53 AM
|