You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
"source": "Mergin Maps API allows you to manage your projects in a simple and effective way.\n\nFirst let's install mergin maps client (if not installed yet)",
38
+
"metadata": {}
39
+
},
40
+
{
41
+
"id": "fb42bae4-f313-4196-9299-2c8d2dacca11",
42
+
"cell_type": "code",
43
+
"source": "!pip install mergin-client",
44
+
"metadata": {
45
+
"trusted": true
46
+
},
47
+
"outputs": [],
48
+
"execution_count": null
49
+
},
50
+
{
51
+
"id": "f7517ef0-7b3f-47f6-8140-c3076ac02215",
52
+
"cell_type": "markdown",
53
+
"source": "Login to Mergin Maps using your an existing user",
54
+
"metadata": {}
55
+
},
56
+
{
57
+
"id": "c380887c-271b-48b6-b435-ed8628dd0a81",
58
+
"cell_type": "code",
59
+
"source": "# Use here your login username and password\nLOGIN=\"\"\nPASSW=\"\"\n\nimport mergin\n\nclient = mergin.MerginClient(login=LOGIN, password=PASSW)",
60
+
"metadata": {
61
+
"trusted": true
62
+
},
63
+
"outputs": [],
64
+
"execution_count": null
65
+
},
66
+
{
67
+
"id": "46bcada4-5dac-44fe-8c63-5f35932404c4",
68
+
"cell_type": "markdown",
69
+
"source": "Let's create a new project (empty) on an existing workspace (fill `WORKSPACE` with an existing workspace)",
"source": "Let's add some random points over Prague city and save it as geopackage. Push the changes on our demo project.",
102
+
"metadata": {}
103
+
},
104
+
{
105
+
"id": "11c18fc4-3202-498f-a733-7bf76f069926",
106
+
"cell_type": "code",
107
+
"source": "# This is needed to compute the geopackage\n!pip install pandas geopandas ",
108
+
"metadata": {
109
+
"trusted": true
110
+
},
111
+
"outputs": [],
112
+
"execution_count": null
113
+
},
114
+
{
115
+
"id": "dbf579be-286d-4453-910a-9ab0d30f4739",
116
+
"cell_type": "code",
117
+
"source": "import geopandas\nfrom shapely.geometry import Point\nimport random\nimport time # For potential delays if making many requests, though geocode handles some.\n\ndef create_random_pois_in_city_gpkg(city_name, num_pois=100, output_gpkg_path=\"city_random_pois.gpkg\", layer_name=\"random_pois\"):\n \"\"\"\n Generates a specified number of random Points of Interest (POIs) within the\n boundaries of a given city and saves them to a GeoPackage file.\n\n Args:\n city_name (str): The name of the city and country (e.g., \"Prague, Czech Republic\").\n num_pois (int): The number of random POIs to generate.\n output_gpkg_path (str): The file path for the output GeoPackage.\n layer_name (str): The name for the layer within the GeoPackage.\n \"\"\"\n try:\n # Step 1: Geocode the city to get its boundary\n print(f\"Attempting to geocode '{city_name}' to fetch its boundary...\")\n # Nominatim requires a user_agent. geopandas.tools.geocode passes it to geopy.\n # If you encounter issues, ensure geopy is installed and up-to-date.\n # A unique user_agent is good practice for repeated use.\n city_gdf = geopandas.tools.geocode(city_name, provider=\"nominatim\", user_agent=f\"random_poi_generator_{random.randint(1000,9999)}\")\n\n if city_gdf.empty:\n print(f\"Error: Could not geocode '{city_name}'. Please check the city name or your internet connection.\")\n return\n\n # Assuming the first result is the correct boundary for the city\n city_boundary_polygon = city_gdf.geometry.iloc[0]\n print(f\"Successfully obtained boundary for '{city_name}'. CRS: {city_gdf.crs}\")\n\n # Step 2: Get the bounding box of the city to generate random points\n min_lon, min_lat, max_lon, max_lat = city_boundary_polygon.bounds\n\n generated_points = []\n poi_attributes = {'id': [], 'name': [], 'category': []}\n\n print(f\"Generating {num_pois} random POIs within '{city_name}'...\")\n\n # Safety counter to prevent potential infinite loops for complex geometries\n max_attempts_per_poi = 1000\n total_attempts = 0\n\n while len(generated_points) < num_pois:\n if total_attempts > num_pois * max_attempts_per_poi:\n print(f\"Warning: Exceeded maximum attempts. Generated {len(generated_points)} out of {num_pois} POIs.\")\n break\n\n # Generate a random point within the bounding box\n random_longitude = random.uniform(min_lon, max_lon)\n random_latitude = random.uniform(min_lat, max_lat)\n candidate_point = Point(random_longitude, random_latitude)\n\n # Check if the generated point is actually within the city's polygon (not just its bounding box)\n if candidate_point.within(city_boundary_polygon):\n generated_points.append(candidate_point)\n poi_id = len(generated_points)\n poi_attributes['id'].append(poi_id)\n poi_attributes['name'].append(f\"POI_{city_name.split(',')[0].replace(' ','_')}_{poi_id}\")\n poi_attributes['category'].append(random.choice([\"Attraction\", \"Restaurant\", \"Shop\", \"Park\", \"Service\"]))\n\n if len(generated_points) % 10 == 0:\n print(f\"Generated {len(generated_points)}/{num_pois} POIs...\")\n\n total_attempts += 1\n\n if not generated_points:\n print(\"No POIs could be generated within the city boundary. Exiting.\")\n return\n\n # Step 3: Create a GeoDataFrame from the generated points and attributes\n # The CRS of the points should match the CRS of the geocoded city boundary\n pois_gdf = geopandas.GeoDataFrame(poi_attributes, geometry=generated_points, crs=city_gdf.crs)\n print(f\"Created GeoDataFrame with {len(pois_gdf)} POIs.\")\n\n # Step 4: Save the GeoDataFrame to a GeoPackage file\n print(f\"Saving POIs to '{output_gpkg_path}' in layer '{layer_name}'...\")\n pois_gdf.to_file(output_gpkg_path, layer=layer_name, driver=\"GPKG\")\n print(f\"Successfully created GeoPackage: '{output_gpkg_path}'\")\n\n except ImportError:\n print(\"Error: One or more required libraries (geopandas, shapely, geopy) are not installed.\")\n print(\"Please install them using: pip install geopandas shapely geopy\")\n except Exception as e:\n print(f\"An unexpected error occurred: {e}\")\n print(\"Please ensure you have an active internet connection for geocoding.\")\n\nif __name__ == \"__main__\":\n # Define the city and parameters\n target_city = \"Prague, Czech Republic\"\n number_of_pois_to_generate = 500\n output_file_name = \"/tmp/demo-prague/prague_random_points.gpkg\"\n output_layer_name = \"prague_pois\"\n\n create_random_pois_in_city_gpkg(\n city_name=target_city,\n num_pois=number_of_pois_to_generate,\n output_gpkg_path=output_file_name,\n layer_name=output_layer_name\n )",
"source": "Let's say, now your project is on a 'template' state and you want to create many projects from here. Simply clone the project using the API!",
"source": "Let's delete one of the cloned projects. \n\nNOTE: using `delete_project_now` will bypass the default value `DELETED_PROJECT_EXPIRATION`. See: https://merginmaps.com/docs/server/environment/#data-synchronisation-and-management",
0 commit comments