In [1]:
"""
Write d211 for EMME from Shapefiles
"""
Out[1]:
In [2]:
nodeShp = r'D:\TRENMO_JASP\CARRIS\emme\add_opp_sul_tejo\nodes_sul_tejo_diss2.shp'
arcShp = r'D:\TRENMO_JASP\CARRIS\emme\add_opp_sul_tejo\rdv_emmev5_sul_tejo.shp'
nodeID = 'node_id'
nodeX = 'emme_x'
nodeY = 'emme_y'
arcNodeStart = 'node_start'
arcNodeEnd = 'node_end'
arcModes = 'modo'
arcType = 'tipo_via'
arcLen = 'LENGTH'
arcFlow = 'DIR_TRAVEL'
arcLane = 'lanes'
outFile = r'D:\TRENMO_JASP\CARRIS\emme\emme_files_jasp\d211_sul_tejo.in'
In [3]:
import codecs
from gasp.fromshp import shp_to_df
In [4]:
# Convert to GeoPandas
nodeDf = shp_to_df(nodeShp)
arcDf = shp_to_df(arcShp)
In [5]:
def add_wspaces(df, col, left=True):
df["len_id"] = df[col].astype(str).str.len()
max_len = df.loc[df["len_id"].idxmax()].len_id
df.len_id = max_len - df.len_id
df["ws"] = " "
df.ws = df.ws.str.repeat(df.len_id)
if left:
df[col] = df.ws + df[col].astype(str)
else:
df[col] = df[col].astype(str) + df.ws
df.drop(["ws", "len_id"], axis=1, inplace=True)
return df
In [6]:
# Prepare nodes to be writen
nodeDf = add_wspaces(nodeDf, nodeID)
nodeDf = add_wspaces(nodeDf, nodeX)
nodeDf = add_wspaces(nodeDf, nodeY)
In [7]:
col4 = " 0"
col5 = " 0"
col6 = " 0"
col7 = "0000"
In [8]:
nodeDf["lnh"] = "a " + nodeDf[nodeID] + " " + nodeDf[nodeX] + " " + \
nodeDf[nodeY] + " " + col4 + " " + col5 + " " + \
col6 + " " + col7
In [9]:
# Prepare arcs to be written
# Fix To-From
arcDf.loc[arcDf[arcFlow] == 'T', "start_id_fix"] = arcDf[arcNodeEnd]
arcDf.loc[arcDf[arcFlow] != 'T', "start_id_fix"] = arcDf[arcNodeStart]
arcDf.loc[arcDf[arcFlow] == 'T', "end_id_fix"] = arcDf[arcNodeStart]
arcDf.loc[arcDf[arcFlow] != 'T', "end_id_fix"] = arcDf[arcNodeEnd]
# Duplicate both travelling
bothDf = arcDf.loc[arcDf[arcFlow] =='B']
bothDf["start_id_fix"] = arcDf[arcNodeEnd]
bothDf["end_id_fix"] = arcDf[arcNodeStart]
# Add duplicates to main Arcs DataFrame
arcDf = arcDf.append(bothDf, ignore_index=True)
arcDf.drop([arcNodeEnd, arcNodeStart], axis=1, inplace=True)
arcDf.rename(columns={
"start_id_fix" : "start_id", "end_id_fix" : "end_id"
}, inplace=True)
arcDf = add_wspaces(arcDf, "start_id")
arcDf = add_wspaces(arcDf, "end_id")
In [10]:
arcDf["dist"] = arcDf[arcLen].round(2)
arcDf["dist"] = arcDf["dist"].astype(str)
In [11]:
arcDf = add_wspaces(arcDf, "dist", left=None)
arcDf[arcModes] = arcDf[arcModes].astype(str)
arcDf = add_wspaces(arcDf, arcModes, left=None)
arcDf[arcType] = arcDf[arcType].astype(int)
arcDf[arcType] = arcDf[arcType].astype(str)
arcDf["auxtype"] = arcDf[arcType].str[-1]
arcDf[arcLane] = arcDf[arcLane].astype(int)
arcDf[arcLane] = arcDf[arcLane].astype(str)
arcDf[arcLane] = arcDf[arcLane] + ".0"
In [12]:
arcDf["lnh"] = "a " + arcDf.start_id + " " + arcDf.end_id + " " + \
arcDf.dist + " " + arcDf[arcModes] + (" " * 7) + \
arcDf[arcType] + " " + arcDf[arcLane] + " " + \
arcDf.auxtype + " 0 0 0"
In [13]:
print arcDf["lnh"][0]
In [14]:
# Write File
with codecs.open(outFile, "w", encoding='utf-8') as txt:
txt.write((
u"c Emme Module: 2.14(v9.04) "
u"Date: {} "
u"User: EC98/TRENMO.....cm\n"
).format(u"18-07-02 11:30"))
txt.write((
u"c Project: AML2018"
u" "
u" \n"
))
txt.write((
u"c Scenario 1: Cenario Base"
u" "
u" \n"
))
# Write nodes
txt.write(u"t nodes init\n")
writeNodes = nodeDf["lnh"].values.tolist()
for row in writeNodes:
txt.write(u"{}\n".format(unicode(row)))
# Write links/arcs
txt.write(u"\nt links init\n")
writeLinks = arcDf["lnh"].values.tolist()
for row in writeLinks:
txt.write(u"{}\n".format(unicode(row)))
txt.close()