Skip to content

Commit

Permalink
added script to extract population data
Browse files Browse the repository at this point in the history
  • Loading branch information
rakow committed Aug 6, 2024
1 parent 88357c9 commit c9ca320
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 13 deletions.
20 changes: 7 additions & 13 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ MEMORY ?= 20G
JAR := matsim-$(N)-*.jar

kyoto := ../public-svn/matsim/scenarios/countries/jp/kyoto
confidential := ../shared-svn/projects/matsim-kyoto

osmosis := osmosis/bin/osmosis

Expand All @@ -24,43 +25,36 @@ input/kansai.osm.pbf:

input/network.osm: input/kansai.osm.pbf

# FIXME: Adjust level of details and area

$(osmosis) --rb file=$<\
--tf accept-ways bicycle=yes highway=motorway,motorway_link,trunk,trunk_link,primary,primary_link,secondary_link,secondary,tertiary,motorway_junction,residential,unclassified,living_street\
--bounding-polygon file="../shared-svn/projects/$N/data/area.poly"\
--bounding-polygon file="input/area.poly"\
--used-node --wb input/network-detailed.osm.pbf

$(osmosis) --rb file=$<\
--tf accept-ways highway=motorway,motorway_link,trunk,trunk_link,primary,primary_link,secondary_link,secondary,tertiary,motorway_junction\
--bounding-box top=51.92 left=11.45 bottom=50.83 right=13.36\
--used-node --wb input/network-coarse.osm.pbf

$(osmosis) --rb file=$<\
--tf accept-ways highway=motorway,motorway_link,motorway_junction,trunk,trunk_link,primary,primary_link\
--used-node --wb input/network-germany.osm.pbf

$(osmosis) --rb file=input/network-germany.osm.pbf --rb file=input/network-coarse.osm.pbf --rb file=input/network-detailed.osm.pbf\
--merge --merge\
$(osmosis) --rb file=input/network-coarse.osm.pbf --rb file=input/network-detailed.osm.pbf\
--merge\
--tag-transform file=input/remove-railway.xml\
--wx $@

rm input/network-detailed.osm.pbf
rm input/network-coarse.osm.pbf
rm input/network-germany.osm.pbf


input/sumo.net.xml: input/network.osm

$(SUMO_HOME)/bin/netconvert --geometry.remove --ramps.guess --ramps.no-split\
--type-files $(SUMO_HOME)/data/typemap/osmNetconvert.typ.xml,$(SUMO_HOME)/data/typemap/osmNetconvertUrbanDe.typ.xml\
--type-files $(SUMO_HOME)/data/typemap/osmNetconvert.typ.xml\
--tls.guess-signals true --tls.discard-simple --tls.join --tls.default-type actuated\
--junctions.join --junctions.corner-detail 5\
--roundabouts.guess --remove-edges.isolated\
--no-internal-links --keep-edges.by-vclass passenger,bicycle\
--remove-edges.by-vclass hov,tram,rail,rail_urban,rail_fast,pedestrian\
--output.original-names --output.street-names\
--proj "+proj=utm +zone=53 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"\
--proj "+proj=utm +zone=53 +datum=WGS84 +units=m +no_defs"\
--lefthand true
--osm-files $< -o=$@


Expand Down
Binary file added input/area.gpkg
Binary file not shown.
48 changes: 48 additions & 0 deletions input/area.poly
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
26101001001
1
135.73306751966228 34.87073344970929
135.73136033233789 34.87075299638351
135.72974358990282 34.87120532106917
135.59762830922838 34.923964796171695
135.59625597408223 34.924804541820436
135.59511567458847 34.92575628050391
135.59436440996362 34.926941605825384
135.5940682681097 34.92825627406589
135.55374249771842 35.1360553509106
135.5537017047248 35.137798206491034
135.55507396099628 35.154786309421894
135.5551811918037 35.15600937780549
135.55568575318512 35.157163475622625
135.584450355535 35.220492859394284
135.58508235914425 35.221573285574834
135.5854822787441 35.222073257668065
135.58689642543368 35.22327641771856
135.63373787093332 35.25976016576515
135.63428707280005 35.260154583120915
135.63629113691238 35.26157299086489
135.63661612131972 35.26174798609984
135.79227684372395 35.32516809539943
135.79373704613968 35.32566494849434
135.79530938467258 35.32580087577925
135.79686394802118 35.32556464681516
135.7988485911786 35.32501195332199
135.8105242695204 35.321299774608505
135.81189106881797 35.32070288935732
135.86079475870204 35.293247205920665
135.8621412834626 35.292452461528406
135.86313733948296 35.29136458518591
135.86504228537342 35.28738939817372
135.86615870500023 35.28404059023343
135.86629592495538 35.282670565935724
135.8841505317859 34.94744801911249
135.8841462130628 34.94734667094723
135.8838632494947 34.94606591651803
135.88314767698824 34.944905119127014
135.8830855117753 34.94483258961723
135.881914550447 34.94383615747787
135.84888627371598 34.92639040728104
135.84754883545472 34.92569676588468
135.73469888281187 34.871148584446594
135.73306751966228 34.87073344970929
END
END
71 changes: 71 additions & 0 deletions src/main/python/extract_population_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import argparse
import os

import numpy as np
from matsim.scenariogen.data import TripMode, preparation, run_create_ref_data
from matsim.scenariogen.data.preparation import join_person_with_household, remove_persons_with_invalid_trips, \
create_activities


def transform_persons(df):

# Filter on the shape file for kyoto
# "PREF" = 26 AND "CITY" IN (101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111)

df = df[df.location.isin(("26101", "26102", "26103", "26104", "26105", "26106", "26107", "26108", "26109", "26110", "26111"))]

df.loc[:, "age"] = preparation.cut(df.age, [0, 12, 18, 25, 35, 66, np.inf])

return df


def trip_filter(df):
# Motorcycles are counted as cars
df.loc[df.main_mode == TripMode.MOTORCYCLE, "main_mode"] = TripMode.CAR

# Other mode are ignored in the total share
df = df[df.main_mode != TripMode.OTHER]

return df


if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Converter for survey data")

parser.add_argument("-d", "--directory",
help="Directory with the kyoto survey data",
default=os.path.expanduser(
"~/Development/matsim-scenarios/shared-svn/projects/matsim-kyoto/data/survey_2010"))

parser.add_argument("--output", default="table", help="Output prefix")

args = parser.parse_args()

result = run_create_ref_data.create(
args.directory, transform_persons, trip_filter,
invalid_trip_handling=run_create_ref_data.InvalidHandling.REMOVE_TRIPS,
ref_groups=["age"]
)

print(result.share)

hh, persons, trips = (result.all_hh, result.all_persons, result.all_trips)

# Motorcycles are counted as cars
trips.loc[trips.main_mode == TripMode.MOTORCYCLE, "main_mode"] = TripMode.CAR

hh.to_csv(args.output + "-households.csv")
trips.to_csv(args.output + "-trips.csv")

df = join_person_with_household(persons, hh)
df = remove_persons_with_invalid_trips(df, trips)

df.to_csv(args.output + "-persons.csv")

print("Written survey csvs with %d persons" % len(df))

activities = create_activities(df, trips, include_person_context=False, cut_groups=False)
activities.to_csv(args.output + "-activities.csv", index=False)

0 comments on commit c9ca320

Please sign in to comment.