void urbandevelWaterAttributes::run()
{
    DM::System * sys = this->getData("data");

    std::vector<DM::Component *> cities = sys->getAllComponentsInView(city);
    std::vector<DM::Component *> buildings = sys->getAllComponentsInView(buildingview);

    if (cities.size() != 1 && paramfromCity)
    {
        DM::Logger(DM::Warning) << "One component expected. There are " << cities.size();
        return;
    }

    DM::Component * currentcity = cities[0];

    if (paramfromCity)
    {
        dwfpp = currentcity->getAttribute("dwfpp")->getDouble();
        wsdpp = currentcity->getAttribute("wsdpp")->getDouble();
    }

    for (int i = 0; i < buildings.size(); i++)
    {
        DM::Face * currentbuilding = dynamic_cast<DM::Face *>(buildings[i]);
        int pop = static_cast<int>(currentbuilding->getAttribute("POP")->getDouble());

        if (pop < 1) continue;

        double dwf = pop*dwfpp;
        double wsd = pop*wsdpp;

        currentbuilding->addAttribute("dwf", dwf);
        currentbuilding->addAttribute("wsd", wsd);
    }
}
Esempio n. 2
0
void Current_RealisationModule::run()
{
    DM::System * sys = this->getData("City");
    DM::Component * cmp = new DM::Component();
    sys->addComponent(cmp,Simu);

    cmp->addAttribute("MusicFileNo",atoi(this->RealisationNr.c_str()));
}
void urbandevelReduction::run()
{
    // get data from stream/port
    DM::System * sys = this->getData("data");

    std::vector<DM::Component *> cities = sys->getAllComponentsInView(city);
    if (cities.size() != 1)
    {
        DM::Logger(DM::Warning) << "Only one component expected. There are " << cities.size();
        return;
    }

    DM::Component * currentcity = cities[0];

    std::vector<std::string> type;
    type.push_back("res");
    type.push_back("com");
    type.push_back("ind");

    int cyclepopdiff  = static_cast<int>(currentcity->getAttribute("cyclepopdiff")->getDouble());
    int cyclecomdiff  = static_cast<int>(currentcity->getAttribute("cyclecomdiff")->getDouble());
    int cycleinddiff  = static_cast<int>(currentcity->getAttribute("cycleinddiff")->getDouble());

    bool dev = 1;

    DM::Logger(DM::Warning) << "popdiff = " << cyclepopdiff;

    for (int i = 0; i < type.size() ; ++i)
    {
        cities[0]->changeAttribute("triggerbool",0);

        if (cyclepopdiff > 0)
        {
            cities[0]->changeAttribute("triggerbool",1);
            setdev(type[i],dev);
        }
    }
}
void ImportRasterData2::run()
{
	DM::System * sys = this->getData("Data");
	DM::View data(dataname, DM::RASTERDATA, DM::WRITE);
	DM::RasterData * r = this->getRasterData("Data", data);
	QFile file(QString::fromStdString(FileName));



	DM::Component * cmp = new DM::Component();
	sys->addComponent(cmp,Coords);

	QTextStream stream(&file);
	if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
		DM::Logger(DM::Error) << "warning, read input file ";
		return;
	}

	QString line("NULL");

	int LineCounter  = 0;
	int rowCounter = 0;
	int ncols = 0;
	int nrows = 0;
	double xoffset = 0;
	double yoffset = 0;
	double cellsize = 0;
	double NoDataValue = -9999; //default

	//Read Header
	while (!line.isNull() && LineCounter < 6 ) {
		LineCounter++;
		line =stream.readLine();
		if (LineCounter == 1) {
			QStringList list = line.split(QRegExp("\\s+"));
			QString s = QString(list[1]);
			s.replace(",", ".");
			ncols = s.toInt();
		}
		if (LineCounter == 2) {
			QStringList list = line.split(QRegExp("\\s+"));
			QString s = QString(list[1]);
			s.replace(",", ".");
			nrows = s.toInt();
		}
		if (LineCounter == 3) {
			QStringList list = line.split(QRegExp("\\s+"));
			QString s = QString(list[1]);
			s.replace(",", ".");
			xoffset = s.toDouble();
			cmp->addAttribute("Xoffset",xoffset);
		}
		if (LineCounter == 4) {
			QStringList list = line.split(QRegExp("\\s+"));
			QString s = QString(list[1]);
			s.replace(",", ".");
			yoffset = s.toDouble();
			cmp->addAttribute("Yoffset",yoffset);
		}
		if (LineCounter == 5) {
			QStringList list = line.split(QRegExp("\\s+"));
			QString s = QString(list[1]);
			s.replace(",", ".");
			cellsize = s.toDouble() * multiplier;
		}
		if (LineCounter == 6) {
			QStringList list = line.split(QRegExp("\\s+"));
			QString s = QString(list[1]);
			s.replace(",", ".");
			NoDataValue = s.toDouble();
		}
	}
	std::cout <<" Cols " << ncols << std::endl;
	std::cout <<" Rows " << nrows << std::endl;
	std::cout <<" Cellsize " << cellsize << std::endl;
	r->setNoValue(NoDataValue);

	r->setSize(ncols, nrows, cellsize,cellsize,xoffset,yoffset);

	while (!line.isNull()) {
		LineCounter++;
		line =stream.readLine();
		if (LineCounter >= 6 && rowCounter < nrows) {
			QStringList list = line.split(QRegExp("\\s+"));
			for ( int i = 0; i < list.size(); i++ ) {
				QString s = QString(list[i]);
				s.replace(",", ".");
				if (flip)
					r->setCell(i, nrows-rowCounter-1, s.toDouble());
				else
					r->setCell(i, rowCounter, s.toDouble());
			}
			rowCounter++;

		}
	}
	file.close();
}
void urbandevelReduction::setdev(std::string type, bool dev)
{
    DM::System * sys = this->getData("data");
    std::vector<DM::Component *> sb = sys->getAllComponentsInView(superblock);
    std::vector<DM::Component *> cb = sys->getAllComponentsInView(cityblock);
    std::vector<DM::Component *> prcl = sys->getAllComponentsInView(parcel);

    std::vector<int> sbrankvec; //

    std::string rankfieldname = "redrank";
    if (dev) rankfieldname = "devrank";

    // develop all available parcels, no matter which ranking (fill up existing superblocks)

    for (int i = 0; i < prcl.size(); i++)
    {
        std::string status = prcl[i]->getAttribute("status")->getString();\
        std::string prcltype = prcl[i]->getAttribute("type")->getString();

        DM::Logger(DM::Debug) << "analyzing parcel, status: " << status << "; type: " << prcltype;

        if (prcltype != type || status == "populated" ) continue;

        if (status == "empty") {
            prcl[i]->changeAttribute("status", "develop");
            DM::Logger(DM::Warning) << "setting parcel to develop";
            return;
        }
    }

    // develop all abvailable cityblocks, no matter which ranking (fill up existing superblocks))

    for (int i = 0; i < cb.size(); i++)
    {
        std::string status = cb[i]->getAttribute("status")->getString();
        std::string cbtype = cb[i]->getAttribute("type")->getString();

        DM::Logger(DM::Debug) << "analyzing cb, status: " << status << "; type: " << cbtype;
        if (cbtype != type || status == "populated" ) continue;

        if (status == "empty") {
            cb[i]->changeAttribute("status", "develop");
            DM::Logger(DM::Debug) << "setting cityblock to develop";
            return;
        }
    }

    for (int i = 0; i < sb.size(); i++)
    {
        int rank = static_cast<int>(sb[i]->getAttribute(rankfieldname)->getDouble());
        sbrankvec.push_back(rank);
    }

    int maxrank = *std::max_element(sbrankvec.begin(), sbrankvec.end());

    for (int i = 0; i < sb.size(); i++)
    {
        std::string status = sb[i]->getAttribute("status")->getString();
        std::string sbtype = sb[i]->getAttribute("type")->getString();

        DM::Logger(DM::Debug) << "analyzing sb, status: " << status << "; type: " << sbtype;

        int sbrank = static_cast<int>(sb[i]->getAttribute(rankfieldname)->getDouble());

        if ( sbrank < maxrank || sbtype != type || status == "populated" ) continue;

        if (status == "empty") {
            sb[i]->changeAttribute("status", "develop");
            DM::Logger(DM::Warning) << "setting superblock to develop";
            return;
        }
    }
    return;
}
Esempio n. 6
0
void SinglePoint::run() {
	DM::System * sys = this->getData("sys");
	sys->addNode(x,y,z, DM::View(viewname, DM::NODE, DM::WRITE));

}
Esempio n. 7
0
void ModernBuilding::run()
{
	std::vector<double> roofColor;
	roofColor.push_back(0.66);
	roofColor.push_back(0.66);
	roofColor.push_back(0.66);
	std::vector<double> wallColor;
	wallColor.push_back(196./255.);
	wallColor.push_back(196./255.);
	wallColor.push_back(196./255.);
	std::vector<double> windowColor;
	windowColor.push_back(0.53);
	windowColor.push_back(0.81);
	windowColor.push_back(1);

	//Get simulation datastream
	DM::System * city = this->getData("city");

	//Create building container
	DM::Component * building = city->addComponent(new Component(), buildings);

	//Set building parameters
	building->addAttribute("type", 1001);
	building->addAttribute("built_year", buildyear);
	building->addAttribute("stories", stories);
	building->addAttribute("stories_below", 0); //cellar counts as story
	building->addAttribute("stories_height",3 );

	building->addAttribute("floor_area", l*b);
	building->addAttribute("gross_floor_area", l * b * stories);
	building->addAttribute("V_living", l*b*stories * 3);

	building->addAttribute("l_bounding", l);
	building->addAttribute("b_bounding", b);
	building->addAttribute("h_bounding", stories * 3);

	building->addAttribute("alpha_bounding", 0);

	building->addAttribute("alpha_roof", 0);

	building->addAttribute("cellar_used", 1);
	building->addAttribute("roof_used", 0);

	building->addAttribute("T_heating", heatingT);
	building->addAttribute("T_cooling", 26);


	//Building Footprint. Orientation matters. Check if the normal vector of the face is in your direction
	std::vector<DM::Node * > nodes_footprint;

	DM::Node * n1  = city->addNode(-l/2.,  -b/2., 0.);
	DM::Node * n2  = city->addNode( l/2.,  -b/2., 0.);
	DM::Node * n3  = city->addNode( l/2.,   b/2., 0.);
	DM::Node * n4  = city->addNode(-l/2.,   b/2., 0.);

	nodes_footprint.push_back(n1);
	nodes_footprint.push_back(n2);
	nodes_footprint.push_back(n3);
	nodes_footprint.push_back(n4);
	nodes_footprint.push_back(n1); //To a closed polygon last is first

	//Overhang
	DM::Node n1_c (-l/2. - overhang,  -b/2. - overhang, 0.);
	DM::Node n2_c ( l/2. + overhang,  -b/2. - overhang, 0.);
	DM::Node n3_c ( l/2. + overhang,   b/2. + overhang, 0.);
	DM::Node n4_c (-l/2. - overhang,   b/2. + overhang, 0.);

	std::vector<DM::Node> overhang_nodes;
	overhang_nodes.push_back(n1_c);
	overhang_nodes.push_back(n2_c);
	overhang_nodes.push_back(n3_c);
	overhang_nodes.push_back(n4_c);


	//Add Footprint
	DM::Face * footprint = city->addFace(nodes_footprint, footprints);

	Node  n = DM::CGALGeometry::CalculateCentroid(city, footprint);
	building->addAttribute("centroid_x", n.getX());
	building->addAttribute("centroid_y", n.getY());

	//Set footprint as floor
	DM::Face * base_plate = city->addFace(nodes_footprint, geometry);
	building->getAttribute("Geometry")->addLink(base_plate, "Geometry");

	//Define baseplate as cellar
	base_plate->addAttribute("type", "ceiling_cellar");

	//Extrude Footprint

	for (int story = 0; story < stories; story++) {
		std::vector<DM::Face*> extruded_faces = TBVectorData::ExtrudeFace(city, geometry, nodes_footprint, 3);
		//last face in vector is ceiling
		int unsigned lastID = extruded_faces.size()-1;
		for (unsigned int i = 0; i < extruded_faces.size(); i++) {
			DM::Face * f = extruded_faces[i];
			//if wall
			if (i != lastID) {
				//Add type
				f->addAttribute("type", "wall_outside");
				//Add color
				f->getAttribute("color")->setDoubleVector(wallColor);
				//Create Windows every 5m with = 1.5m height = 1.0m
				std::vector<DM::Face* > windows = LittleGeometryHelpers::CreateHolesInAWall(city, f, w_distance, w_width, w_height, parapet);
				foreach (DM::Face * w, windows) {
					city->addComponentToView(w, this->geometry);
					w->addAttribute("type", "window");
					w->getAttribute("color")->setDoubleVector(windowColor);
					building->getAttribute("Geometry")->addLink(w, "Geometry");
				}
			}
			//if ceiling
			else if (story != stories -1){
void urbandevelBuilding::run()
{
    DM::System * sys = this->getData("data");
    std::vector<DM::Component *> cities = sys->getAllComponentsInView(cityview);
    std::vector<DM::Component *> parcels = sys->getAllComponentsInView(parcelview);

    if (cities.size() != 1 && paramfromCity)
    {
        DM::Logger(DM::Warning) << "One component expected. There are " << cities.size();
        return;
    }

    DM::Component * currentcity = cities[0];

    if (paramfromCity)
    {
        buildingyear = static_cast<int>(currentcity->getAttribute("year")->getDouble());
        offset = static_cast<int>(currentcity->getAttribute("ratio")->getDouble());
    }

    int cyclediff = -1;
    std::string triggertype = "";

    if (genPopulation)
    {
        triggertype = currentcity->getAttribute("triggertype")->getString();

        //DM::Logger(DM::Warning) << "pop " << cyclepopdiff << ";com " << cyclecomdiff << ";ind " << cycleinddiff;
        if ( !buildingtype.empty() && !triggertype.empty() )
        {
            if ( buildingtype.compare("res") == 0 && buildingtype.compare(triggertype ) == 0)
            {
                cyclediff = static_cast<int>(currentcity->getAttribute("cyclepopdiff")->getDouble());
            }
            else if ( buildingtype == "com" && buildingtype.compare(triggertype) == 0 ) cyclediff = static_cast<int>(currentcity->getAttribute("cyclecomdiff")->getDouble());
            else if ( buildingtype == "ind" && buildingtype.compare(triggertype) == 0 ) cyclediff = static_cast<int>(currentcity->getAttribute("cycleinddiff")->getDouble());
        }
    }

    int currentyear = static_cast<int>(currentcity->getAttribute("currentyear")->getDouble());
    int startyear = static_cast<int>(currentcity->getAttribute("startyear")->getDouble());

    if ( currentyear <= startyear+1 )
    {
        std::vector<DM::Component *> buildings = sys->getAllComponentsInView(buildingview);
        for (int i = 0; i < buildings.size(); i++)
        {
            std::string bdyear = buildings[i]->getAttribute("year")->getString();
            if ( bdyear.empty() )
            {
                QString year = QString::number(startyear) + QString("-1-1");
                std::string stdyear = year.toUtf8().constData();
                buildings[i]->changeAttribute("year", stdyear);
            }
        }
    }

    int numberOfHouseBuild = 0;
    int numberOfPeople = 0;

    for (int i = 0; i < parcels.size(); i++)
    {
        DM::Face * currentparcel = dynamic_cast<DM::Face *>(parcels[i]);
        double parcel_area = TBVectorData::CalculateArea((DM::System*)sys, (DM::Face*)currentparcel);
        if (parcel_area < 300)
        {
            DM::Logger(DM::Debug) << "BD: parcel to small";
            currentparcel->addAttribute("status","recreation");
            continue;
        }
        std::string parcelstatus = currentparcel->getAttribute("status")->getString();
        std::string parceltype = currentparcel->getAttribute("type")->getString();
        int parcelheight_avg = static_cast<int>(currentparcel->getAttribute("height_avg")->getDouble());
        int parcelheight_min = static_cast<int>(currentparcel->getAttribute("height_min")->getDouble());
        int parcelheight_max = static_cast<int>(currentparcel->getAttribute("height_max")->getDouble());

        std::string checkstatus = "process";

        // do not generate houses if no population (if population should be generated) is available
        // OR no parcel status equals develop (if development should happen on signal)

        //DM::Logger(DM::Warning) << "CYCLEDIFF = " << cyclediff << "TYPE = " << buildingtype << parceltype;

        if ( genPopulation && cyclediff <= 0 )
            continue;

        if ( onSignal )
        {
            if ( parcelstatus.compare(checkstatus) != 0 )
            {
                //DM::Logger(DM::Warning) << "BD: skipping parcel, status = " << parcelstatus;
                continue;
            }
            if (parceltype.compare(buildingtype) != 0 )
            {
                //DM::Logger(DM::Warning) << "BD: skipping parcel, parceltype = " << parceltype << "buidlingtype = " << buildingtype;
                continue;
            }
        }

        std::vector<DM::Face *> f_off = this->createOffest(sys, currentparcel, offset);

        if (f_off.size() == 0)
        {
            currentparcel->addAttribute("status","recreation");
            continue;
        }

        foreach (DM::Face * building, f_off)
        {
            sys->addComponentToView(building, buildingview);



            double roof_area = TBVectorData::CalculateArea((DM::System*)sys, (DM::Face*)building);
            double traffic_area = (parcel_area - roof_area)/3;
            double impervious_area = parcel_area - traffic_area - roof_area;
            int stories = static_cast<int>(parcelheight_min + (rand() % (int)(parcelheight_max - parcelheight_min + 1)));
            DM::Logger(DM::Debug) << "stories - max: " << parcelheight_max << " min: " << parcelheight_min << " calculated: " << stories;

            QString year = QString::number(currentyear) + QString("-1-1");
            std::string stdyear = year.toUtf8().constData();

            building->addAttribute("type", buildingtype);
            building->addAttribute("year", stdyear);
            building->addAttribute("stories", stories);

            building->addAttribute("roofarea", roof_area);
            building->addAttribute("roofarea_effective", 0.8);
            currentparcel->addAttribute("trafficarea", traffic_area);
            currentparcel->addAttribute("trafficarea_effective", 0.9);
            currentparcel->addAttribute("imperviousarea", impervious_area);
            currentparcel->addAttribute("imperviousarea_effective", 0.1);

            //Create Links
            building->getAttribute("PARCEL")->addLink(currentparcel, parcelview.getName());
            currentparcel->getAttribute("BUILDING")->addLink(building, buildingview.getName());

            numberOfHouseBuild++;

            if (genPopulation)
            {
                int peopleinbuilding = static_cast<int>(roof_area * stories / spacepp);
                cyclediff = std::max(cyclediff - peopleinbuilding,0);
                building->addAttribute("POP", peopleinbuilding);
                numberOfPeople = numberOfPeople + peopleinbuilding;

                if ( !buildingtype.empty() )
                {
                    if ( buildingtype.compare("res") == 0)
                    {
                        currentcity->addAttribute("cyclepopdiff", cyclediff);
                    }
                    else if ( buildingtype == "com")
                    {
                        currentcity->addAttribute("cyclecomdiff", cyclediff);
                    }
                    else if ( buildingtype == "ind")
                    {
                        currentcity->addAttribute("cycleinddiff", cyclediff);
                    }
                    else
                    {
                        currentcity->addAttribute("cyclepopdiff", cyclediff);
                    }
                }

            }
            currentparcel->addAttribute("status", "populated");
            building->addAttribute("height", stories*4);
            building->addAttribute("type", buildingtype);
        }

          //DM::Logger(DM::Warning) << "added face(s)";
    }
Esempio n. 9
0
void PlaceJobs::run() {
	DM::System * city = this->getData("City");

	std::vector<Component*> personCmps = city->getAllComponentsInView(persons);
	std::vector<int> person_rand_int_id;
	for (unsigned int i = 0; i < personCmps.size(); i++) {
		person_rand_int_id.push_back(i);
	}
	for (unsigned int i = 0; i < personCmps.size(); i++) {
		int index1 =  rand() % person_rand_int_id.size();
		int index2 =  rand() % person_rand_int_id.size();
		int p_id_tmp = person_rand_int_id[index1];
		person_rand_int_id[index1] = person_rand_int_id[index2];
		person_rand_int_id[index2] = p_id_tmp;

	}



	std::vector<Component*> gridCmps = city->getAllComponentsInView(grids);


	int job_id = 0;
	foreach(DM::Component * grid, gridCmps) 
	{
		double jobsN = grid->getAttribute("jobs")->getDouble();

		if (jobsN < 0.01)
			continue;

		//Calcualte Total Area
		Attribute  * attrBuildings = grid->getAttribute("BUILDING");
		double building_footprints_tot = 0;

		std::vector<Component*> linkedBuildings = attrBuildings->getLinkedComponents();
		foreach(DM::Component* b, linkedBuildings)
			building_footprints_tot += b->getAttribute("area")->getDouble();

		if (building_footprints_tot < 0.01)
			continue;

		//Create Jobs
		double JobperSqft = jobsN/building_footprints_tot;

		foreach(DM::Component* b, linkedBuildings)
		{
			double area = b->getAttribute("area")->getDouble();

			int jobsInBuilding = (int) (area*JobperSqft + 0.5);
			for (int i = 0; i < jobsInBuilding; i++) 
			{
				job_id++;
				DM::Component * job = new DM::Component();

				//Person ID
				DM::Component * person = personCmps[person_rand_int_id[job_id]];
				person->addAttribute("job_id", job_id);

				DM::Component * households = person->getAttribute("HOUSEHOLD")->getLinkedComponents()[0];
				households->addAttribute("workers", households->getAttribute("workers")->getDouble()+1);
				job->addAttribute("id", job_id);
				job->addAttribute("home_based_status", 0);
				job->addAttribute("sector_id", 1);
				job->addAttribute("building_type", 1);

				Attribute lAttr("BUILDING", Attribute::LINK);
				lAttr.addLink(b, "BUILDING");
				job->addAttribute(lAttr);
				city->addComponent(job, jobs);
				Attribute * lBAttr = b->getAttribute("JOB");
				lBAttr->addLink(job, "JOB");

			}
			b->addAttribute("non_residential_sqft", 20*jobsInBuilding*1.1);
		}
void AnnualEmploymentControl::run()
{
	DM::System * sys = this->getData("City");
	int numberOfJobs;

	DM::Component * city = sys->getAllComponentsInView(this->city)[0];
	numberOfJobs = (int)city->getAttribute("JOBS")->getDouble();

	QSqlDatabase db;
	db = QSqlDatabase::addDatabase("QMYSQL", QUuid::createUuid().toString());
	db.setHostName("127.0.0.1");
	db.setUserName("urbansim");
	db.setPassword("urbansim");

	bool ok = db.open();
	if( ok == false) {
		Logger(Error) << "Database failed";
		return;
	}

	// Setup the db and start using it somewhere after successfully connecting to the server..
	QString dbname = QString::fromStdString("urbansim");
	QString tablename = QString::fromStdString("annual_employment_control_totals");

	QSqlQuery query(db);
	bool sr;
	sr = query.exec("USE "+dbname);

	stringstream ss;

	ss << "CREATE TABLE IF NOT EXISTS ";
	ss << tablename.toStdString();
	ss << " (";
	ss << "year" << " "  << "INT";
	ss << ", ";
	ss << "sector_id" << " "  << "INT";
	ss << ", ";
	ss << "home_based_status" << " "  << "INT";
	ss << ", ";
	ss << "number_of_jobs" << " "  << "INT";
	ss << ")";


	Logger(Debug) << ss.str();
	sr = query.exec(QString::fromStdString(ss.str() ));

	if (!sr) {
		Logger(Error) << query.lastError().text().toStdString();

	}
	stringstream insertstream;
	insertstream << "INSERT INTO " << tablename.toStdString() << "(";
	insertstream << "year";
	insertstream << ", ";
	insertstream << "sector_id";
	insertstream << ", ";
	insertstream << "home_based_status";
	insertstream << ", ";
	insertstream << "number_of_jobs";
	insertstream  << ") " << " VALUES (";

	insertstream << "?";
	insertstream << ", ";
	insertstream << "?";
	insertstream << ", ";
	insertstream << "?" ;
	insertstream << ", ";
	insertstream << "?" ;
	insertstream  << ")";



	for (int y = startYear; y <= endYear; y++) {
		query.prepare(QString::fromStdString(insertstream.str()));
		query.addBindValue(y);
		query.addBindValue(1);
		query.addBindValue(0);
		numberOfJobs = (int) numberOfJobs * this->growthRate;
		query.addBindValue(numberOfJobs);
		if ( !query.exec() )
			Logger(Error) << query.lastError().text().toStdString();
	}

	db.close();

}