Skip to content

Commit

Permalink
Merge pull request #31 from daniil2327/main
Browse files Browse the repository at this point in the history
Major update and bbox implementation
  • Loading branch information
louis-e authored Aug 4, 2024
2 parents 838030e + 1f1faeb commit 09a9f1f
Show file tree
Hide file tree
Showing 4 changed files with 212 additions and 78 deletions.
6 changes: 1 addition & 5 deletions src/floodFill.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,7 @@ def floodFill(
):
nxt.append((x, y + 1))

# Timeout (known issue, see Github readme)
if time() - startTimeFloodfill > 7 or (
elementType == "tree_row" and time() - startTimeFloodfill > 0.2
):
return img
# Timeout ei dirst

queue = nxt
if len(nxt) > queueLen:
Expand Down
105 changes: 66 additions & 39 deletions src/getData.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def download_with_wget(url, params, filename):
return filename


def getData(city, state, country, debug, download_method="requests"):
def getData(city, state, country, bbox, file, debug, download_method="requests"):
print("Fetching data...")
api_servers = [
"https://overpass-api.de/api/interpreter",
Expand All @@ -57,63 +57,90 @@ def getData(city, state, country, debug, download_method="requests"):
area[name="{state}"]->.state;
area[name="{country}"]->.country;
(
way(area.country)(area.state)(area.city)[building];
way(area.country)(area.state)(area.city)[highway];
way(area.country)(area.state)(area.city)[landuse];
way(area.country)(area.state)(area.city)[natural];
way(area.country)(area.state)(area.city)[leisure];
way(area.country)(area.state)(area.city)[waterway]["waterway"!="fairway"];
way(area.country)(area.state)(area.city)[amenity];
way(area.country)(area.state)(area.city)[bridge];
way(area.country)(area.state)(area.city)[railway];
way(area.country)(area.state)(area.city)[barrier];
nwr(area.country)(area.state)(area.city)[building];
nwr(area.country)(area.state)(area.city)[highway];
nwr(area.country)(area.state)(area.city)[landuse];
nwr(area.country)(area.state)(area.city)[natural];
nwr(area.country)(area.state)(area.city)[leisure];
nwr(area.country)(area.state)(area.city)[waterway]["waterway"!="fairway"];
nwr(area.country)(area.state)(area.city)[amenity];
nwr(area.country)(area.state)(area.city)[bridge];
nwr(area.country)(area.state)(area.city)[railway];
nwr(area.country)(area.state)(area.city)[barrier];
);
(._;>;);
out;
"""
elif bbox:
bbox = bbox.split(",")
bbox = [float(i) for i in bbox]
print(bbox)

query1 = f"""
[out:json][bbox:{bbox[0]},{bbox[1]},{bbox[2]},{bbox[3]}];
(
nwr["building"];
nwr["highway"];
nwr["landuse"];
nwr["natural"];
nwr["leisure"];
nwr["waterway"];
nwr["amenity"];
nwr["bridge"];
nwr["railway"];
nwr["barrier"];
);
(._;>;);
out;
"""
elif file:
print("Loading data from file")
else:
query1 = f"""
[out:json];
area[name="{city}"]->.city;
area[name="{country}"]->.country;
(
way(area.country)(area.city)[building];
way(area.country)(area.city)[highway];
way(area.country)(area.city)[landuse];
way(area.country)(area.city)[natural];
way(area.country)(area.city)[leisure];
way(area.country)(area.city)[waterway]["waterway"!="fairway"];
way(area.country)(area.city)[amenity];
way(area.country)(area.city)[bridge];
way(area.country)(area.city)[railway];
way(area.country)(area.city)[barrier];
nwr(area.country)(area.city)[building];
nwr(area.country)(area.city)[highway];
nwr(area.country)(area.city)[landuse];
nwr(area.country)(area.city)[natural];
nwr(area.country)(area.city)[leisure];
nwr(area.country)(area.city)[waterway]["waterway"!="fairway"];
nwr(area.country)(area.city)[amenity];
nwr(area.country)(area.city)[bridge];
nwr(area.country)(area.city)[railway];
nwr(area.country)(area.city)[barrier];
);
(._;>;);
out;
"""

print(f"Chosen server: {url}")
try:
filename = "arnis-debug-raw_data.json"
if download_method == "requests":
file_path = download_with_requests(url, {"data": query1}, filename)
elif download_method == "curl":
file_path = download_with_curl(url, {"data": query1}, filename)
elif download_method == "wget":
file_path = download_with_wget(url, {"data": query1}, filename)
if file:
with open("data.json") as dataset:
data = json.load(dataset)
else:
print("Invalid download method. Using 'requests' by default.")
file_path = download_with_requests(url, {"data": query1}, filename)
print(f"Chosen server: {url}")
filename = "arnis-debug-raw_data.json"
if download_method == "requests":
file_path = download_with_requests(url, {"data": query1}, filename)
elif download_method == "curl":
file_path = download_with_curl(url, {"data": query1}, filename)
elif download_method == "wget":
file_path = download_with_wget(url, {"data": query1}, filename)
else:
print("Invalid download method. Using 'requests' by default.")
file_path = download_with_requests(url, {"data": query1}, filename)

if file_path is None:
os._exit(1)
if file_path is None:
os._exit(1)

with open(file_path, "r") as file:
data = json.load(file)
with open(file_path, "r") as file:
data = json.load(file)

if len(data["elements"]) == 0:
print("Error! No data available")
os._exit(1)
if len(data["elements"]) == 0:
print("Error! No data available")
os._exit(1)

except Exception as e:
if "The server is probably too busy to handle your request." in str(e):
Expand Down
87 changes: 53 additions & 34 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
parser.add_argument("--city", dest="city", help="Name of the city")
parser.add_argument("--state", dest="state", help="Name of the state")
parser.add_argument("--country", dest="country", help="Name of the country")
parser.add_argument("--bbox", dest="bbox", help="Bounding box of the city")
parser.add_argument("--file", dest="file", help="JSON file containing OSM data")
parser.add_argument("--path", dest="path", help="Path to the minecraft world")
parser.add_argument(
"--downloader",
Expand All @@ -39,18 +41,23 @@
help="Enable debug mode",
)
args = parser.parse_args()
if args.city is None or args.country is None or args.path is None:
print("Error! Missing arguments")
os._exit(1)
if args.city is None or args.state is None or args.country is None or args.path is None:
if args.bbox is None and args.file is None:
print("Error! Missing arguments")
os._exit(1)

gc.collect()
np.seterr(all="raise")
np.set_printoptions(threshold=sys.maxsize)

processStartTime = time.time()
air = anvil.Block("minecraft", "air")
glass = anvil.Block("minecraft", "glass_pane")
brick = anvil.Block("minecraft", "bricks")
stone = anvil.Block("minecraft", "stone")
grass_block = anvil.Block("minecraft", "grass_block")
spruce_log = anvil.Block("minecraft", "spruce_log")
birch_leaves = anvil.Block("minecraft", "birch_leaves")
dirt = anvil.Block("minecraft", "dirt")
sand = anvil.Block("minecraft", "sand")
podzol = anvil.Block.from_numeric_id(3, 2)
Expand All @@ -62,8 +69,9 @@
potatoes = anvil.Block("minecraft", "potatoes")
cobblestone = anvil.Block("minecraft", "cobblestone")
iron_block = anvil.Block("minecraft", "iron_block")
log = anvil.Block.from_numeric_id(17)
leaves = anvil.Block.from_numeric_id(18)
oak_log = anvil.Block.from_numeric_id(17)
oak_leaves = anvil.Block.from_numeric_id(18)
birch_log = anvil.Block("minecraft", "birch_log")
white_stained_glass = anvil.Block("minecraft", "white_stained_glass")
dark_oak_door_lower = anvil.Block(
"minecraft", "dark_oak_door", properties={"half": "lower"}
Expand All @@ -73,7 +81,12 @@
)
cobblestone_wall = anvil.Block("minecraft", "cobblestone_wall")
stone_brick_slab = anvil.Block.from_numeric_id(44, 5)

red_flower = anvil.Block.from_numeric_id(38)
yellow_flower = anvil.Block("minecraft","dandelion")
blue_flower = anvil.Block("minecraft", "blue_orchid")
white_flower = anvil.Block("minecraft", "azure_bluet")

white_concrete = anvil.Block("minecraft", "white_concrete")
black_concrete = anvil.Block("minecraft", "black_concrete")
gray_concrete = anvil.Block("minecraft", "gray_concrete")
Expand Down Expand Up @@ -120,13 +133,13 @@ def saveRegion(region="all"):
regions[region].save(mcWorldPath + "/region/" + region + ".mca")
print(f"Saved {region}")


from .tree import createTree
def run():
if not (os.path.exists(mcWorldPath + "/region")):
print("Error! No Minecraft world found at given path")
os._exit(1)

rawdata = getData(args.city, args.state, args.country, args.debug, args.downloader)
rawdata = getData(args.city, args.state, args.country, args.bbox, args.file, args.debug, args.downloader)
imgarray = processData(rawdata, args)

print("Generating minecraft world...")
Expand All @@ -150,7 +163,7 @@ def run():
for j in i:
setBlock(dirt, x, 0, z)
if j == 0: # Ground
setBlock(light_gray_concrete, x, 1, z)
setBlock(grass_block, x, 1, z)
elif j == 10: # Street
setBlock(black_concrete, x, 1, z)
setBlock(air, x, 2, z)
Expand Down Expand Up @@ -185,35 +198,41 @@ def run():
if randomChoice == 0 or randomChoice == 1:
setBlock(grass, x, 2, z)
elif j == 31: # Farmland
randomChoice = randint(0, 16)
if randomChoice == 0:
setBlock(water, x, 1, z)
else:
setBlock(farmland, x, 1, z)
randomChoice = randint(0, 2)
if randomChoice == 0:
setBlock(wheat, x, 2, z)
elif randomChoice == 1:
setBlock(carrots, x, 2, z)
setBlock(grass_block, x, 1, z)
randomChoice = randint(0, 20)
randomTree = randint(1, 3)
randomFlower = randint(1, 4)
if randomChoice == 20:
createTree(x, z, randomTree)
elif randomChoice == 2:
if randomFlower == 1:
setBlock(red_flower, x, 2, z)
elif randomFlower == 2:
setBlock(blue_flower, x, 2, z)
elif randomFlower == 3:
setBlock(yellow_flower, x, 2, z)
else:
setBlock(potatoes, x, 2, z)
setBlock(white_flower, x, 2, z)
elif randomChoice == 0 or randomChoice == 1:
setBlock(grass, x, 2, z)
elif j == 32: # Forest
setBlock(grass_block, x, 1, z)
randomChoice = randint(0, 8)
if randomChoice >= 0 and randomChoice <= 5:
randomChoice = randint(0, 20)
randomTree = randint(1, 3)
randomFlower = randint(1, 4)
if randomChoice == 20:
createTree(x, z, randomTree)
elif randomChoice == 2:
if randomFlower == 1:
setBlock(red_flower, x, 2, z)
elif randomFlower == 2:
setBlock(blue_flower, x, 2, z)
elif randomFlower == 3:
setBlock(yellow_flower, x, 2, z)
else:
setBlock(white_flower, x, 2, z)
elif randomChoice == 0 or randomChoice == 1:
setBlock(grass, x, 2, z)
elif randomChoice == 6:
fillBlocks(log, x, 2, z, x, 8, z)
fillBlocks(leaves, x - 2, 5, z - 2, x + 2, 6, z + 2)
setBlock(air, x - 2, 6, z - 2)
setBlock(air, x - 2, 6, z + 2)
setBlock(air, x + 2, 6, z - 2)
setBlock(air, x + 2, 6, z + 2)
fillBlocks(leaves, x - 1, 7, z - 1, x + 1, 8, z + 1)
setBlock(air, x - 1, 8, z - 1)
setBlock(air, x - 1, 8, z + 1)
setBlock(air, x + 1, 8, z - 1)
setBlock(air, x + 1, 8, z + 1)
elif j == 33: # Cemetery
setBlock(podzol, x, 1, z)
randomChoice = randint(0, 100)
Expand Down Expand Up @@ -324,7 +343,7 @@ def run():
white_concrete, x, 32, z
)

setBlock(glowstone, x, 1, z)
setBlock(white_concrete, x, 1, z)

z += 1
x += 1
Expand Down
Loading

0 comments on commit 09a9f1f

Please sign in to comment.