# Generating OSM Buildings Statistics using Osmosis and OSM Convert
**Table of Contents**
[ToC]
### :memo: Introduction
This is a walkthrough on how to generate statistics for OpenStreetMap features in a certain boundary using osmosis and osm convert.
### Step 1: Install Osmosis and osmconvert
**Osmosis** is a command line Java application for processing OSM data. You can install it by following the instructions [**here**](https://wiki.openstreetmap.org/wiki/Osmosis/Installation) depending on the platform you are using.
**osmconvert** is used to convert and process ==OpenStreetMap== files. You can download it and install it following the instructions [**here**](https://wiki.openstreetmap.org/wiki/Osmconvert#Download).
:rocket:
### Step 2: Download OpenStreetMap Data
Download OpenStreetMap data for your country or region in .pbf format from [Geofabrik](http://download.geofabrik.de/). The latest data for Uganda used in this example can be downloaded from [here](http://download.geofabrik.de/africa/uganda.html).
---
### Step 3: Convert boundary data to Polyfiles
You need to convert vector polygons to poly-files that can be used by Osmosis, Osmconvert for cliping OpenStreetMap data. There are several ways to do this all documented [here](https://wiki.openstreetmap.org/wiki/Osmosis/Polygon_Filter_File_Format#Converting_to.2Ffrom_POLY_format).
If you do use QGIS, it is easier to use the [OSMPoly_export](https://github.com/nextgis/osmpoly_export) QGIS plugin that will easily generate Poly files from any vector feature(s). Note that it only works with QGIS 2.8.
Copy all your poly files into a folder named **poly**
### Step 4: Create stat and pbf folders
Create a folder named **osmosis** and inside that folder, create sub-folders namely stat, pbf and poly, also put the osmdata file and python script inside the same folder, so it will look like this

### Step 5: Run the script
On the command line, navigate to your folder and run the script using the command
```python
python osmbuildingscript.py
```
The osmbuildingscript
```python
import os
import sys
poly_files = ['Abim','Adjumani','Amolatar','Amuria','Apac','Arua','Budaka','Bugiri','Bukwa','Buliisa','Bundibugyo','Bushenyi','Busia','Namutumba','Butaleja','Dokolo','Gulu','Hoima','Ibanda','Iganga','Jinja','Kaabong','Kabale','Kabarole', 'Kaberamaido','Isingiro','Kalangala','Kaliro','Kampala','Kamuli','Kamwenge','Kanungu','Kapchorwa','Kasese','Katakwi','Kayunga','Kibaale','Kiboga','Amuru','Kiruhura','Kisoro','Kitgum','Koboko','Kotido','Kumi','Kyenjojo','Lira','Luweero','Bududa','Maracha','Masaka','Masindi','Mayuge','Mbale','Mbarara','Mityana','Moroto','Moyo','Mpigi','Mubende','Mukono','Nakapiripirit','Nakaseke','Nakasongola','Nebbi','Ntungamo','Oyam','Pader','Pallisa','Rakai','Rukungiri','Sembabule','Sironko','Soroti','Tororo','Wakiso','Yumbe','Agago','Alebtong','Amudat','Buhweju','Buikwe','Bukedea','Bukomansimbi','Bulambuli','Butambala','Buvuma','Buyende','Gomba','Kalungu','Kibuku','Kiryandongo','Kole','Kween','Kyankwanzi','Kyegegwa','Lamwo','Luuka','Lwengo','Lyantonde','Manafwa','Mitooma','Namayingo','Napak','Ngora','Ntoroko','Nwoya','Otuke','Rubirizi','Serere','Sheema','Zombo','Kagadi','Kakumiro','Omoro','Rubanda','Namisindwa','Pakwach','Butebo','Rukiga','Kyotera','Bunyangabu','Nabilatuk','Bugweri','Kasanda','Kwania','Kapelebyong','Kibuube','Obongi','Kazo','Rwampara','Kitagwenda','Madi-Okollo','Karenga','Lusot']
for poly in poly_files:
# clipping data into pbf data
clip2province = "osmconvert uganda-latest.osm.pbf -B=poly/{0}.poly -o=pbf\{0}.pbf".format(poly)
os.system(clip2province)
# get permission pbf data
# permission = "chmod +r pbf/*"
# os.system(permission)
# filtering osm data based on building
filterbuilding = "osmosis --read-pbf pbf/{0}.pbf --tf accept-ways building=* --tf reject-nodes --tf reject-relations --write-pbf pbf/{0}_building.pbf".format(poly)
os.system(filterbuilding)
# showing osm data statistic
osmstats = "osmconvert pbf/{0}_building.pbf --out-statistics > stat/{0}_all.stat".format(poly)
os.system(osmstats)
```
:::info
:bulb: The python script above does the following
* Uses **osmconvert** to clip data into the pbf boundary area and store it the pbf folder
* Uses **Osmosis** to filter osm data based on buildings and store them in the pbf folder
* Uses **osmconvert** to generate buildings data statistics and store them in the stat folder
:::
### Output
