use multiple libraries in python and create a complete beautiful 3d model of a Earth
- User
use multiple libraries in python and create a complete beautiful 3d model of a Earth
Assistant
To create a beautiful 3D model of Earth using Python, we'll use pyvista for 3D rendering, rasterio to handle elevation data, and PIL to process the texture. Here's the complete code with explanations:
Step 1: Install Required Libraries
pip install pyvista rasterio PillowStep 2: Download Required Files
- Blue Marble Texture: Download here and save as
BlueMarble.jpg. - Elevation Data (DEM): Download here (e.g.,
DEM.tif).
Step 3: Python Code
import pyvista as pv import numpy as np from PIL import Image import rasterio # Load the Blue Marble texture texture = Image.open('BlueMarble_200407.jpg').convert('RGBA') texture_data = np.array(texture) # Create a sphere with resolution matching the texture and elevation data sphere = pv.Sphere(radius=1, theta_resolution=360, phi_resolution=180) # Read elevation data (replace with your DEM file path) with rasterio.open('DEM.tif') as src: elevation = src.read(1) # First band (elevation) transform = src.transform # Function to convert latitude/longitude to pixel coordinates def lat_lon_to_pixel(lat, lon): row, col = rasterio.transform.rowcol(transform, lon, lat) # Handle out-of-bounds by clamping to valid range row = np.clip(row, 0, elevation.shape[0]-1) col = np.clip(col, 0, elevation.shape[1]-1) return row, col # Get sphere vertices and compute their latitude/longitude points = sphere.points latitudes = [] longitudes = [] for point in points: x, y, z = point r = np.linalg.norm(point) # Polar angle (theta) from z-axis to latitude theta = np.arccos(z / r) lat = 90 - (theta * 180 / np.pi) # Azimuthal angle (phi) to longitude phi = np.arctan2(y, x) * 180 / np.pi latitudes.append(lat) longitudes.append(phi) # Compute UV coordinates for texture mapping uvs = [] for lat, lon in zip(latitudes, longitudes): u = (lon + 180) / 360.0 # Longitude to [0, 1] v = (90 - lat) / 180.0 # Latitude to [0, 1] uvs.append([u, v]) sphere.texture_map_points = np.array(uvs) # Apply elevation displacement elevations = [] for lat, lon in zip(latitudes, longitudes): row, col = lat_lon_to_pixel(lat, lon) elev = elevation[row, col] elevations.append(elev) elevations = np.array(elevations) # Scale elevation to Earth's radius (approx 6371 km) # Assuming elevation data is in meters earth_radius_km = 6371 scale_factor = 1e-6 # 1 meter = 1e-6 km displacements = elevations * scale_factor # Displace points radially outward new_points = [] for i, point in enumerate(points): direction = point / np.linalg.norm(point) # Unit vector displacement = direction * displacements[i] new_point = point + displacement new_points.append(new_point) sphere.points = np.array(new_points) # Create plotter with lighting and camera plotter = pv.Plotter() plotter.add_mesh(sphere, texture=texture_data) plotter.set_background('black') plotter.camera_position = 'xz' plotter.show_axes() plotter.show()Explanation:
- Libraries: Uses
pyvistafor 3D rendering,rasteriofor geospatial data, andPILfor image processing. - Texture Mapping: The Blue Marble image is mapped onto the sphere using UV coordinates derived from latitude/longitude.
- Elevation Data: Elevation values from a DEM file are scaled and applied as radial displacements to create topographic relief.
- Visualization: The model includes realistic lighting and a dark background for a clean aesthetic.
Notes:
- Adjust
theta_resolution/phi_resolutionfor smoother rendering (higher values increase detail but slow down rendering). - Ensure the DEM file's coordinate system matches the sphere's latitude/longitude for accurate elevation mapping.
- Modify
scale_factorto exaggerate or reduce terrain height.
This code will generate a visually stunning 3D Earth model with realistic topography and texture.
- Blue Marble Texture: Download here and save as
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.