สร้างคําขอชั้นข้อมูล

ปลายทาง dataLayers จะให้ข้อมูลพลังงานแสงอาทิตย์โดยละเอียดสำหรับภูมิภาครอบตำแหน่งที่ระบุ ปลายทางจะแสดงผลไฟล์ TIFF ที่ดาวน์โหลดได้ 17 ไฟล์ ได้แก่

  • โมเดลแพลตฟอร์มดิจิทัล (DSM)
  • เลเยอร์ผสม RGB (ภาพถ่ายทางอากาศ)
  • เลเยอร์มาสก์ที่ระบุขอบเขตของการวิเคราะห์
  • ฟลักซ์แสงอาทิตย์ประจำปีหรือผลตอบแทนต่อปีของพื้นผิวหนึ่งๆ
  • ฟลักซ์แสงอาทิตย์หรือผลตอบแทนรายเดือนของพื้นผิวหนึ่งๆ
  • ร่มเงารายชั่วโมง (24 ชั่วโมง)

ดูข้อมูลเพิ่มเติมเกี่ยวกับคำจำกัดความของความไหลลื่นของ Solar API ได้ที่แนวคิดของ Solar API

เกี่ยวกับคำขอชั้นข้อมูล

ตัวอย่างต่อไปนี้แสดง URL ของคำขอ REST ไปยังเมธอด dataLayers

https://solar.googleapis.com/v1/dataLayers:get?parameters

ใส่พารามิเตอร์ของ URL คำขอที่ระบุข้อมูลต่อไปนี้

  • พิกัดละติจูดและลองจิจูดของตำแหน่ง
  • รัศมีของภูมิภาคที่อยู่รอบๆ สถานที่ตั้ง
  • ข้อมูลชุดย่อยของข้อมูลที่จะแสดง (DSM, RGB, มาสก์, ฟลักซ์รายปี หรือฟลักซ์รายเดือน)
  • คุณภาพขั้นต่ำที่อนุญาตในผลลัพธ์
  • ขนาดข้อมูลขั้นต่ำที่จะแสดงในหน่วยเมตรต่อพิกเซล

ตัวอย่างคำขอชั้นข้อมูล

ตัวอย่างต่อไปนี้ขอข้อมูลเชิงลึกเกี่ยวกับอาคารทั้งหมดภายในรัศมี 100 เมตรของสถานที่ตั้งที่พิกัดละติจูด = 37.4450 และลองจิจูด = -122.1390

คีย์ API

ในการสร้างคำขอไปยัง URL ในการตอบกลับ ให้เพิ่มคีย์ API ของคุณต่อท้าย URL ดังนี้

curl -X GET "https://solar.googleapis.com/v1/dataLayers:get?location.latitude=37.4450&location.longitude=-122.1390&radiusMeters=100&view=FULL_LAYERS&requiredQuality=HIGH&exactQualityRequired=true&pixelSizeMeters=0.5&key=YOUR_API_KEY"

นอกจากนี้ คุณยังสร้างคำขอ HTTP ได้โดยวาง URL ในคำขอ cURL ลงในแถบ URL ของเบราว์เซอร์ การส่งคีย์ API จะทำให้คุณใช้งานและวิเคราะห์ข้อมูลได้ดียิ่งขึ้น รวมถึงควบคุมการเข้าถึงข้อมูลการตอบกลับได้ดียิ่งขึ้น

โทเค็น OAuth

หมายเหตุ: รูปแบบนี้ใช้สำหรับสภาพแวดล้อมการทดสอบเท่านั้น สำหรับข้อมูลเพิ่มเติม โปรดดูที่ใช้ OAuth

หากต้องการส่งคำขอไปยัง URL ในการตอบกลับ ให้ส่งชื่อโปรเจ็กต์การเรียกเก็บเงินและโทเค็น OAuth ตามขั้นตอนต่อไปนี้

curl -H "Authorization: Bearer $(gcloud auth print-access-token)" \
  -H "X-Goog-User-Project: PROJECT_NUMBER_OR_ID" \
  "https://solar.googleapis.com/v1/dataLayers:get?location.latitude=37.4450&location.longitude=-122.1390&radius_meters=100&required_quality=HIGH&exactQualityRequired=true"
        

TypeScript

หากต้องการส่งคำขอไปยัง URL ในการตอบกลับ ให้ใส่คีย์ API หรือโทเค็น OAuth ในคำขอ ตัวอย่างต่อไปนี้ใช้คีย์ API

/**
 * Fetches the data layers information from the Solar API.
 *   https://developers.google.com/maps/documentation/solar/data-layers
 *
 * @param  {LatLng} location      Point of interest as latitude longitude.
 * @param  {number} radiusMeters  Radius of the data layer size in meters.
 * @param  {string} apiKey        Google Cloud API key.
 * @return {Promise<DataLayersResponse>}  Data Layers response.
 */
export async function getDataLayerUrls(
  location: LatLng,
  radiusMeters: number,
  apiKey: string,
): Promise<DataLayersResponse> {
  const args = {
    'location.latitude': location.latitude.toFixed(5),
    'location.longitude': location.longitude.toFixed(5),
    radius_meters: radiusMeters.toString(),
    // The Solar API always returns the highest quality imagery available.
    // By default the API asks for HIGH quality, which means that HIGH quality isn't available,
    // but there is an existing MEDIUM or LOW quality, it won't return anything.
    // Here we ask for *at least* LOW quality, but if there's a higher quality available,
    // the Solar API will return us the highest quality available.
    required_quality: 'LOW',
  };
  console.log('GET dataLayers\n', args);
  const params = new URLSearchParams({ ...args, key: apiKey });
  // https://developers.google.com/maps/documentation/solar/reference/rest/v1/dataLayers/get
  return fetch(`https://solar.googleapis.com/v1/dataLayers:get?${params}`).then(
    async (response) => {
      const content = await response.json();
      if (response.status != 200) {
        console.error('getDataLayerUrls\n', content);
        throw content;
      }
      console.log('dataLayersResponse', content);
      return content;
    },
  );
}

ช่องและประเภทข้อมูลจะเป็น "type" ใน TypeScript ในตัวอย่างนี้ เรากำหนดประเภทที่กำหนดเองเพื่อจัดเก็บช่องที่สนใจในคำตอบ เช่น ค่าพิกเซลและกรอบล้อมรอบละติจูด/ลองจิจูด คุณใส่ช่องเพิ่มได้ตามต้องการ

export interface GeoTiff {
  width: number;
  height: number;
  rasters: Array<number>[];
  bounds: Bounds;
}

คำจำกัดความของประเภทข้อมูล

ระบบรองรับประเภทข้อมูลต่อไปนี้

export interface DataLayersResponse {
  imageryDate: Date;
  imageryProcessedDate: Date;
  dsmUrl: string;
  rgbUrl: string;
  maskUrl: string;
  annualFluxUrl: string;
  monthlyFluxUrl: string;
  hourlyShadeUrls: string[];
  imageryQuality: 'HIGH' | 'MEDIUM' | 'LOW';
}

export interface Bounds {
  north: number;
  south: number;
  east: number;
  west: number;
}

// https://developers.google.com/maps/documentation/solar/reference/rest/v1/buildingInsights/findClosest
export interface BuildingInsightsResponse {
  name: string;
  center: LatLng;
  boundingBox: LatLngBox;
  imageryDate: Date;
  imageryProcessedDate: Date;
  postalCode: string;
  administrativeArea: string;
  statisticalArea: string;
  regionCode: string;
  solarPotential: SolarPotential;
  imageryQuality: 'HIGH' | 'MEDIUM' | 'LOW';
}

export interface SolarPotential {
  maxArrayPanelsCount: number;
  panelCapacityWatts: number;
  panelHeightMeters: number;
  panelWidthMeters: number;
  panelLifetimeYears: number;
  maxArrayAreaMeters2: number;
  maxSunshineHoursPerYear: number;
  carbonOffsetFactorKgPerMwh: number;
  wholeRoofStats: SizeAndSunshineStats;
  buildingStats: SizeAndSunshineStats;
  roofSegmentStats: RoofSegmentSizeAndSunshineStats[];
  solarPanels: SolarPanel[];
  solarPanelConfigs: SolarPanelConfig[];
  financialAnalyses: object;
}

export interface SizeAndSunshineStats {
  areaMeters2: number;
  sunshineQuantiles: number[];
  groundAreaMeters2: number;
}

export interface RoofSegmentSizeAndSunshineStats {
  pitchDegrees: number;
  azimuthDegrees: number;
  stats: SizeAndSunshineStats;
  center: LatLng;
  boundingBox: LatLngBox;
  planeHeightAtCenterMeters: number;
}

export interface SolarPanel {
  center: LatLng;
  orientation: 'LANDSCAPE' | 'PORTRAIT';
  segmentIndex: number;
  yearlyEnergyDcKwh: number;
}

export interface SolarPanelConfig {
  panelsCount: number;
  yearlyEnergyDcKwh: number;
  roofSegmentSummaries: RoofSegmentSummary[];
}

export interface RoofSegmentSummary {
  pitchDegrees: number;
  azimuthDegrees: number;
  panelsCount: number;
  yearlyEnergyDcKwh: number;
  segmentIndex: number;
}

export interface LatLng {
  latitude: number;
  longitude: number;
}

export interface LatLngBox {
  sw: LatLng;
  ne: LatLng;
}

export interface Date {
  year: number;
  month: number;
  day: number;
}

export interface RequestError {
  error: {
    code: number;
    message: string;
    status: string;
  };
}

API จะแสดงผล URL ในรูปแบบต่อไปนี้

https://solar.googleapis.com/v1/solar/geoTiff:get?id=HASHED_ID

URL เหล่านี้สามารถใช้เข้าถึงไฟล์ GeoTIFF พร้อมข้อมูลที่ขอ

ตัวอย่างการตอบกลับ

คำขอจะสร้างการตอบสนอง JSON ในรูปแบบ:

{
  "imageryDate": {
    "year": 2022,
    "month": 4,
    "day": 6
  },
  "imageryProcessedDate": {
    "year": 2023,
    "month": 8,
    "day": 4
  },
  "dsmUrl": "https://solar.googleapis.com/v1/geoTiff:get?id=6d654a0300e454f4c6db7fff24d7ab98-f51261151c9d4c7e055dd21ce57fa3b5",
  "rgbUrl": "https://solar.googleapis.com/v1/geoTiff:get?id=7c71f407a36c1cd051f5ada9c17a6cb8-4b1a9e2b489656febfb7676f205aea1d",
  "maskUrl": "https://solar.googleapis.com/v1/geoTiff:get?id=814470096c53cb221b524119e1e2700c-ac51cf76452dd6c2e843e6b11922ccc0",
  "annualFluxUrl": "https://solar.googleapis.com/v1/geoTiff:get?id=e044991d7f376dc23f9abe8d4efc909b-982983cd98d0572b9d62ca0a2db38eb3",
  "monthlyFluxUrl": "https://solar.googleapis.com/v1/geoTiff:get?id=9b4638db10d2d58560b9f1e9fb013551-dff565175a1e6861a7afb62ece41e218",
  "hourlyShadeUrls": [
    "https://solar.googleapis.com/v1/geoTiff:get?id=9aa96f4568d2561ad8b6db495b8f8582-da043a2c74541668b3d668e556451e31",
    "https://solar.googleapis.com/v1/geoTiff:get?id=125e26c35e4eb07d385a6868253fb1e3-54fa27bd2c5cd72b79e9f14cf0fa9899",
    ...
  ],
  "imageryQuality": "HIGH"
}

เข้าถึงข้อมูลคำตอบ

การเข้าถึงข้อมูลผ่าน URL การตอบสนองต้องมีการตรวจสอบสิทธิ์เพิ่มเติม หากใช้คีย์การตรวจสอบสิทธิ์ คุณต้องเพิ่มคีย์ API ต่อท้าย URL หากใช้การตรวจสอบสิทธิ์ OAuth คุณต้องเพิ่มส่วนหัว OAuth

คีย์ API

ในการสร้างคำขอไปยัง URL ในการตอบกลับ ให้เพิ่มคีย์ API ของคุณต่อท้าย URL ดังนี้

curl -X GET "https://solar.googleapis.com/v1/solar/geoTiff:get?id=fbde33e9cd16d5fd10d19a19dc580bc1-8614f599c5c264553f821cd034d5cf32&key=YOUR_API_KEY"

นอกจากนี้ คุณยังสร้างคำขอ HTTP ได้โดยวาง URL ในคำขอ cURL ลงในแถบ URL ของเบราว์เซอร์ การส่งคีย์ API จะทำให้คุณใช้งานและวิเคราะห์ข้อมูลได้ดียิ่งขึ้น รวมถึงควบคุมการเข้าถึงข้อมูลการตอบกลับได้ดียิ่งขึ้น

โทเค็น OAuth

หากต้องการส่งคำขอไปยัง URL ในการตอบกลับ ให้ส่งชื่อโปรเจ็กต์การเรียกเก็บเงินและโทเค็น OAuth ตามขั้นตอนต่อไปนี้

curl -X GET \
-H 'X-Goog-User-Project: PROJECT_NUMBER_OR_ID' \
-H "Authorization: Bearer $TOKEN" \
"https://solar.googleapis.com/v1/solar/geoTiff:get?id=fbde33e9cd16d5fd10d19a19dc580bc1-8614f599c5c264553f821cd034d5cf32"
        

TypeScript

ตัวอย่างต่อไปนี้แสดงวิธีรับค่าข้อมูลพิกเซล (ข้อมูลที่จัดเก็บในแต่ละพิกเซลของรูปภาพดิจิทัล รวมถึงค่าสีและแอตทริบิวต์อื่นๆ) คำนวณละติจูดและลองจิจูดจาก GeoTIFF และจัดเก็บไว้ในออบเจ็กต์ TypeScript

สำหรับตัวอย่างเฉพาะนี้ เราเลือกอนุญาตให้ตรวจสอบประเภท ซึ่งช่วยลดข้อผิดพลาดเกี่ยวกับประเภท เพิ่มความน่าเชื่อถือของโค้ด และช่วยให้ดูแลได้ง่ายขึ้น

// npm install geotiff geotiff-geokeys-to-proj4 proj4

import * as geotiff from 'geotiff';
import * as geokeysToProj4 from 'geotiff-geokeys-to-proj4';
import proj4 from 'proj4';

/**
 * Downloads the pixel values for a Data Layer URL from the Solar API.
 *
 * @param  {string} url        URL from the Data Layers response.
 * @param  {string} apiKey     Google Cloud API key.
 * @return {Promise<GeoTiff>}  Pixel values with shape and lat/lon bounds.
 */
export async function downloadGeoTIFF(url: string, apiKey: string): Promise<GeoTiff> {
  console.log(`Downloading data layer: ${url}`);

  // Include your Google Cloud API key in the Data Layers URL.
  const solarUrl = url.includes('solar.googleapis.com') ? url + `&key=${apiKey}` : url;
  const response = await fetch(solarUrl);
  if (response.status != 200) {
    const error = await response.json();
    console.error(`downloadGeoTIFF failed: ${url}\n`, error);
    throw error;
  }

  // Get the GeoTIFF rasters, which are the pixel values for each band.
  const arrayBuffer = await response.arrayBuffer();
  const tiff = await geotiff.fromArrayBuffer(arrayBuffer);
  const image = await tiff.getImage();
  const rasters = await image.readRasters();

  // Reproject the bounding box into lat/lon coordinates.
  const geoKeys = image.getGeoKeys();
  const projObj = geokeysToProj4.toProj4(geoKeys);
  const projection = proj4(projObj.proj4, 'WGS84');
  const box = image.getBoundingBox();
  const sw = projection.forward({
    x: box[0] * projObj.coordinatesConversionParameters.x,
    y: box[1] * projObj.coordinatesConversionParameters.y,
  });
  const ne = projection.forward({
    x: box[2] * projObj.coordinatesConversionParameters.x,
    y: box[3] * projObj.coordinatesConversionParameters.y,
  });

  return {
    // Width and height of the data layer image in pixels.
    // Used to know the row and column since Javascript
    // stores the values as flat arrays.
    width: rasters.width,
    height: rasters.height,
    // Each raster reprents the pixel values of each band.
    // We convert them from `geotiff.TypedArray`s into plain
    // Javascript arrays to make them easier to process.
    rasters: [...Array(rasters.length).keys()].map((i) =>
      Array.from(rasters[i] as geotiff.TypedArray),
    ),
    // The bounding box as a lat/lon rectangle.
    bounds: {
      north: ne.y,
      south: sw.y,
      east: ne.x,
      west: sw.x,
    },
  };
}

ไฟล์ TIFF ทั้งหมดจะแสดงเป็นรูปภาพเปล่าในแอปพลิเคชันโปรแกรมดูรูปภาพ ยกเว้นเลเยอร์ RGB หากต้องการดูไฟล์ TIFF ที่ดาวน์โหลด ให้นําเข้าไฟล์ไปยังซอฟต์แวร์แอปพลิเคชันการแมป เช่น QGIS

ดูข้อกำหนดทั้งหมดของคำขอและคำตอบนี้ได้ในเอกสารอ้างอิง