If you want to use only the building in a radius of a point location then the best will be to first filter osm data and then store it in a postgresql database.
Filter osm pbf file based on location
To process osm pbf data, there are different tools available.
For your goal, I think the best will be to use osmconvert
. Here is the documentation link. You can not set a point and radius but can get the same either setting a bounding box with -b
argument or a polygon stored in a poly file with -B
argument.
osmconvert.exe germany.pbf --max-objects=1000000000 --hash-memory=1000 --max-refs=4000000 -b=10.5,49,11.5,50 -o=nuernberg.osm -v=2
The line of code based on the example from the documentation and adapted to work with large files in Windows environment(max object
max refs
or hash memory
parameters have to be set).
Filter to keep only buildings
Then you have to keep only the buildings, you can use osmfilter
and keep every OSM object that have at least a building key. In the documentation there are many options you can use to drop or keep OSM objects.
osmfilter.exe nuernberg.osm --keep="building=" -o=building.osm --verbose
Store the OSM data in postgresql
I think your code used before will work and will be processed faster :
osm2pgsql -U USER -W -d gis -H 127.0.0.1 --number-processes 24 -C 2048 building.osm --hstore
ST_DWithin
and the usage of CRS units (GIS.SE is a good place for that).