Advertisement
JanuszKowalski123

10. geometry 1

May 9th, 2024 (edited)
9
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. CREATE TABLE Renewable_Energy_Devices (
  2. device_id NVARCHAR(26) PRIMARY KEY,
  3. device_status INT CHECK (device_status >= 0 AND device_status <= 6),
  4. device_power DECIMAL(18,0),
  5. company_id INT,
  6. device_location GEOGRAPHY,
  7. device_location_geom GEOMETRY,
  8. device_hierarchy HIERARCHYID,
  9. device_description XML
  10. );
  11.  
  12. DECLARE @latitude DECIMAL(9, 6), @longitude DECIMAL(9, 6);
  13. DECLARE @point GEOMETRY;
  14. DECLARE @device_hierarchy HIERARCHYID;
  15. DECLARE @device_description XML;
  16.  
  17. DECLARE @i INT = 1;
  18. WHILE @i <= 50
  19. BEGIN
  20. SET @latitude = RAND() * 180 - 90;
  21. SET @longitude = RAND() * 360 - 180;
  22. SET @point = GEOMETRY::Point(@latitude, @longitude, 4326);
  23.  
  24. -- Tworzenie hierarchii z użyciem losowej wartości dodanej do korzenia
  25. IF @i = 1
  26. SET @device_hierarchy = HIERARCHYID::GetRoot().GetDescendant(NULL, NULL);
  27. ELSE
  28. SET @device_hierarchy = @device_hierarchy.GetDescendant(NULL, NULL);
  29.  
  30. SET @device_description = '<description>Device_' + CAST(@i AS NVARCHAR(5)) + '</description>';
  31.  
  32. INSERT INTO Renewable_Energy_Devices (device_id, device_status, device_power, company_id, device_location, device_location_geom, device_hierarchy, device_description)
  33. VALUES (
  34. 'Device_' + CAST(@i AS NVARCHAR(5)),
  35. CAST(RAND() * 6 AS INT),
  36. CAST(RAND() * 1000 AS DECIMAL(18, 0)),
  37. CAST(RAND() * 10 AS INT) + 1,
  38. GEOGRAPHY::Point(@latitude, @longitude, 4326),
  39. @point,
  40. @device_hierarchy,
  41. @device_description
  42. );
  43.  
  44. SET @i = @i + 1;
  45. END;
  46.  
  47. Zapytanie wybierze wszystkie urządzenia z tabeli Renewable_Energy_Devices, których geometria umiejscowienia (kolumna device_location_geom) znajduje się w odległości mniejszej niż 1000 jednostek od punktu o współrzędnych (0, 0).
  48.  
  49. -- Wybierz wszystkie urządzenia, których geometria umiejscowienia jest w odległości mniejszej niż 1000 jednostek od punktu o współrzędnych (0, 0)
  50. SELECT *
  51. FROM Renewable_Energy_Devices
  52. WHERE device_location_geom.STDistance(geometry::Point(0, 0, 4326)) < 1000;
  53.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement