bool TripData::Get_Zone_Data (Zone_File &file, Zone_Data &data) { if (Data_Service::Get_Zone_Data (file, data)) { int zone = data.Zone (); //---- count the records for the data file ----- for (Data_Itr data_itr = data_group.begin (); data_itr != data_group.end (); data_itr++) { Db_Sort_Array *data = data_itr->data_db; int field = data_itr->zone_field; if (field >= 0) { zone = file.Get_Integer (field); } if (zone < 0) continue; if (data->Read_Record (zone)) { int count = data->Get_Integer (1); data->Put_Field (1, ++count); data->Write_Record (zone); } } return (true); } return (false); }
void ZoneData::Read_Data (void) { int id, field, count; Db_Sort_Array *data; Db_Header *file; Data_Itr data_itr; for (data_itr = data_group.begin (); data_itr != data_group.end (); data_itr++) { file = data_itr->file; data = data_itr->data_db; field = data_itr->join_field; //---- read the data file ---- Show_Message (String ("Reading %s -- Record") % file->File_Type ()); Set_Progress (); count = 0; while (file->Read ()) { Show_Progress (); //---- get the join field id ---- id = file->Get_Integer (field); if (id == 0) continue; //---- copy the data fields ---- data->Copy_Fields (*file); //---- set the record index and zone count ---- data->Put_Field (0, id); data->Put_Field (1, 0); //---- save the database record ---- if (!data->Write_Record (id)) { Error ("Writing Data File Database"); } count++; } End_Progress (); Print (2, String ("Number of Data #%d Records = %d") % data_itr->group % count); file->Close (); } }
void LocationData::Set_Files (void) { int i, nfld, key; bool binary; Db_Field *fld; Db_Base *file; Db_Sort_Array *data; Data_Itr data_itr; Polygon_Itr poly_itr; //---- create the program data structure ---- data_rec.push_back (input_file); data_rec.push_back (output_file); //---- convert each data file to binary ---- for (data_itr = data_group.begin (); data_itr != data_group.end (); data_itr++) { file = data_itr->file; data = data_itr->data_db; data_rec.push_back ((Db_Base *) data); data->File_ID (file->File_ID ()); data->File_Type (file->File_Type ()); nfld = file->Num_Fields (); key = data_itr->join_field; binary = (file->Record_Format () == BINARY); fld = file->Field (key); data->Add_Field (fld->Name (), DB_INTEGER, 10); data->Add_Field ("AL_COUNT", DB_INTEGER, 10); for (i=0; i < nfld; i++) { if (i == key) continue; fld = file->Field (i); if (fld != 0) { data->Add_Field (fld->Name (), fld->Type (), fld->Size (), fld->Units (), binary); } } } //---- read the arcview boundary file ---- for (poly_itr = polygons.begin (); poly_itr != polygons.end (); poly_itr++) { file = poly_itr->file; data = poly_itr->data_db; data_rec.push_back ((Db_Base *) data); data->File_ID (file->File_ID ()); data->File_Type (file->File_Type ()); nfld = file->Num_Fields (); binary = (file->Record_Format () == BINARY); data->Add_Field ("INDEX", DB_INTEGER, 10); for (i=0; i < nfld; i++) { fld = file->Field (i); if (fld != 0) { data->Add_Field (fld->Name (), fld->Type (), fld->Size (), fld->Units (), binary); } } } Write (1, "Compiling Conversion Script"); if (Report_Flag (PRINT_SCRIPT)) { Header_Number (PRINT_SCRIPT); if (!Break_Check (10)) { Print (1); Page_Header (); } } program.Initialize (data_rec, random.Seed () + 1); if (!program.Compile (program_file, Report_Flag (PRINT_SCRIPT))) { Error ("Compiling Conversion Script"); } if (Report_Flag (PRINT_STACK)) { Header_Number (PRINT_STACK); program.Print_Commands (false); } Header_Number (0); }
void ZoneData::Sum_Zone () { int i, num, field, zone, z; double x0, y0, dx, dy, distance; Db_Sort_Array *data; Sum_Itr sum_itr; Zone_Itr zone_itr; //---- initialize the database ----- for (sum_itr = sum_group.begin (); sum_itr != sum_group.end (); sum_itr++) { data = sum_itr->data_db; for (zone_itr = zone_array.begin (); zone_itr != zone_array.end (); zone_itr++) { zone = zone_itr->Zone (); data->Reset_Record (); data->Put_Field (0, zone); if (!data->Write_Record (zone)) { Error ("Writing Sum Distance Database"); } } } //---- reopen the activity location file ---- input_file->Open (0); Show_Message (String ("Summing %s -- Record") % input_file->File_Type ()); Set_Progress (); while (input_file->Read ()) { Show_Progress (); zone = input_file->Zone (); if (zone == 0) continue; x0 = Scale (input_file->X ()); y0 = Scale (input_file->Y ()); for (zone_itr = zone_array.begin (); zone_itr != zone_array.end (); zone_itr++) { z = zone_itr->Zone (); if (z == zone) continue; dx = zone_itr->X () - x0; dy = zone_itr->Y () - y0; distance = sqrt (dx * dx + dy * dy); for (sum_itr = sum_group.begin (); sum_itr != sum_group.end (); sum_itr++) { if (distance <= sum_itr->distance) { data = sum_itr->data_db; if (!data->Read_Record (z)) continue; num = data->Num_Fields (); data->Put_Field (1, data->Get_Integer (1) + 1); for (i=2; i < num; i++) { field = sum_itr->field_map [i]; data->Put_Field (i, data->Get_Double (i) + input_file->Get_Double (field)); } data->Write_Record (z); } } } } End_Progress (); }
void LocationData::Write_Location (void) { int index, field, loc, num_in, num_out; int x, y, zone, zone_field, join; double dx, dy, weight, weight1, weight2, distance; Dtime time; Db_Sort_Array *data; Location_Data *location_ptr; Link_Data *link_ptr; Loc_Walk_Data *loc_walk_ptr; Subzone_Data *subzone_ptr; Sub_Group_Itr sub_itr; Data_Itr data_itr; Points_Map_Itr boundary_itr; Points_Map *polygon; Polygon_Itr poly_itr; Int2_Key key, key2; Int2_Map_Itr int2_itr; Int_Map_Itr int_itr; I2_Dbl_Map_Itr wt_itr; Int_Dbl_Map_Itr loc_itr; Int_Itr fld_itr, code_itr; num_in = num_out = 0; //---- reopen the activity location file ---- input_file->Open (0); zone_field = input_file->Zone_Field (); Show_Message (String ("Writing %s -- Record") % output_file->File_Type ()); Set_Progress (); while (input_file->Read ()) { Show_Progress (); loc = input_file->Location (); if (loc == 0) continue; int_itr = location_map.find (loc); index = int_itr->second; location_ptr = &location_array [index]; num_in++; output_file->Copy_Fields (*input_file); //---- copy standard field types ----- output_file->Location (loc); output_file->Link (input_file->Link ()); output_file->Dir (input_file->Dir ()); output_file->Offset (input_file->Offset ()); output_file->Setback (input_file->Setback ()); //---- get the zone number ---- zone = location_ptr->Zone (); if (zone_file_flag && zone >= 0) { zone = zone_array [zone].Zone (); } output_file->Zone (zone); //---- set the link use flag field ---- if (use_flag) { int_itr = link_map.find (output_file->Link ()); if (int_itr != link_map.end ()) { link_ptr = &link_array [int_itr->second]; code_itr = use_code.begin (); for (fld_itr = use_field.begin (); fld_itr != use_field.end (); fld_itr++, code_itr++) { if ((link_ptr->Use () & (*code_itr)) > 0) { output_file->Put_Field (*fld_itr, 1); } else { output_file->Put_Field (*fld_itr, 0); } } } } //---- calculate the walk access field ---- if (walk_access_flag) { loc_walk_ptr = &loc_walk_array [index]; output_file->Put_Field (walk_access_field, loc_walk_ptr->weight); } //---- calculate the subzone fields ---- for (sub_itr = sub_group.begin (); sub_itr != sub_group.end (); sub_itr++) { if (sub_itr->loc_field < 0) continue; output_file->Put_Field (sub_itr->loc_field, 0); //---- allocate subzone data ---- if (subzone_map_flag) { loc_itr = sub_itr->loc_weight.find (loc); if (loc_itr != sub_itr->loc_weight.end ()) { weight = loc_itr->second; } else { weight = 0; } } else if (sub_itr->max_distance == 0) { weight = 0; key.first = zone; key.second = 0; for (int2_itr = sub_itr->data_map.lower_bound (key); int2_itr != sub_itr->data_map.end (); int2_itr++) { if (int2_itr->first.first != zone) break; subzone_ptr = &sub_itr->data [int2_itr->second]; if (subzone_ptr->Data () == 0) continue; key2.first = int2_itr->first.second; key2.second = loc; wt_itr = subzone_weight.find (key2); if (wt_itr != subzone_weight.end () && subzone_ptr->Data () > 0) { weight += subzone_ptr->Data () * wt_itr->second; } } if (weight < 0) weight = 0; } else { //---- distance weighted attribute ---- x = location_ptr->X (); y = location_ptr->Y (); weight1 = weight2 = 0; key.first = zone; key.second = 0; for (int2_itr = sub_itr->data_map.lower_bound (key); int2_itr != sub_itr->data_map.end (); int2_itr++) { if (int2_itr->first.first != zone) break; subzone_ptr = &sub_itr->data [int2_itr->second]; dx = UnRound (subzone_ptr->X () - x); dy = UnRound (subzone_ptr->Y () - y); distance = sqrt (dx * dx + dy * dy); if (distance < sub_itr->max_distance) { weight = subzone_ptr->Data () * (sub_itr->max_distance - distance); if (weight > weight1) { weight2 = weight1; weight1 = weight; } else if (weight > weight2) { weight2 = weight; } } } weight = (weight1 + weight2) / 2.0; } output_file->Put_Field (sub_itr->loc_field, weight); } //---- set the data field id ---- if (script_flag) { for (data_itr = data_group.begin (); data_itr != data_group.end (); data_itr++) { data = data_itr->data_db; field = data_itr->loc_field; if (field == zone_field) { join = zone; } else { join = input_file->Get_Integer (field); } if (!data->Read_Record (join)) { data->Reset_Record (); } } //---- set the polygon record index ---- if (polygon_flag) { dx = UnRound (location_ptr->X ()); dy = UnRound (location_ptr->Y ()); for (poly_itr = polygons.begin (); poly_itr != polygons.end (); poly_itr++) { data = poly_itr->data_db; polygon = &poly_itr->polygon; join = 0; for (boundary_itr = polygon->begin (); boundary_itr != polygon->end (); boundary_itr++) { if (In_Polygon (boundary_itr->second, dx, dy)) { join = boundary_itr->first; break; } } if (join == 0 || !data->Read_Record (join)) { data->Reset_Record (); } } } //---- execute the conversion script ---- if (program.Execute (num_in) == 0) continue; } //---- save the output fields ---- if (!output_file->Write ()) { Error (String ("Writing %s Record %d") % output_file->File_Type () % loc); } num_out++; } End_Progress (); input_file->Close (); output_file->Close (); }