Ddongyu1009init
cdf6f0e0创建于 2024年9月6日历史提交
/**
  This file is created by Yu Dong (2023).
  License  : Apache 2.0
  FileName : Geometry.cj
  Author   : Yu Dong
  Version  : 0.0.1
  Date     : 20230424
  Note     : First Commit
 */

/**
 * @file
 * Geometry
 */
package gistools.geometry

import std.collection.*
import std.math.*
import encoding.json.*
import encoding.json.stream.*
import serialization.serialization.*

import gistools.*
import gistools.base.*


/**
 * Geometry for common methods.
 * @author Yu Dong
 * @since 0.38.2
 */
public abstract class Geometry <: ToJson & ToString {

    // Spatial Reference, default is Unknown
    public var spatialReference : SpatialReference = GeographicSpatialReference.Unknown

    
    /**
     * The Function is to determine if the geometry has spatial reference
     *
     * @return Type of Bool
     * @since 0.38.2
     */
    public func hasSpatialReference() : Bool {
        !this.spatialReference.isUnknown()
    }

    // to json
    public func toJson(): JsonValue

    /**
     * The Function is to transfrom geometry from data model to json value
     *
     * @param jv of JsonValue
     *
     * @return Type of DataModel
     * @since 0.38.2
     */
    public static func fromJson(jv: JsonValue): DataModel {
        
        try {
            let geometry = jv.asObject().get("geometry").getOrThrow().asObject()
            let geometry_type = geometry.get("type").getOrThrow().asString().getValue()

            match(geometry_type) {
                case "Point" => Point.fromJson(jv)
                case "Polyline" => Polyline.fromJson(jv)
                case "Polygon" => Polygon.fromJson(jv)
                case _ => DataModelNull()
            }
        }catch (e : Exception) {
            DataModelNull()
        }
    }
}

/**
 * CoordinateTransformer Interface
 * 
 * @author Yu Dong
 * @since 0.38.2
 */
public interface CoordinateTransformer<T> where T <: Geometry{

    // transfrom to GCJ-02
    mut func toGCJ02() : ?T
    
    // transfrom to BD-09
    mut func toBD09() : ?T
    
    // transfrom to WGS 1984
    mut func toWGS1984() : ?T

    // project the geographic to mercator
    mut func projectToMercator() : ?T
    
}