2010-06-28 12 views
5

मैं कुछ भौगोलिक स्थान जावा कोड http://janmatuschek.de/LatitudeLongitudeBoundingCoordinates#Java (नीचे दिखाया गया) से पाइथन पर पोर्ट कर रहा हूं। इसे दो कार्यों (डिग्री से या रैडियंस से) का उपयोग करके प्रारंभ किया जा सकता है। मैंने सोचा कि मैं की तरहक्या मेरे पास अजगर वर्ग में दो init फ़ंक्शन हो सकते हैं?

class geoLocation: 

    _radLat = 0 
    _radLong = 0 
    _degLat = 0 
    _degLong = 0 


    def fromDegrees(lat, long): 
     #set _radLat, _radLong, _degLat, _degLong 

    def fromRadians(lat, long): 
     #set _radLat, _radLong, _degLat, _degLong 

    ... 

कुछ कर सकते हैं लेकिन यह इष्टतम नहीं लगता है के बाद से मैं दो बार _radLat, _radLong, _degLat और _degLong के लिए मूल्यों की स्थापना की। क्या मैं दो init कार्यों को परिभाषित कर सकता हूँ? ऐसा करने का सबसे अच्छा तरीका क्या है?

धन्यवाद

/** 
* <p>Represents a point on the surface of a sphere. (The Earth is almost 
* spherical.)</p> 
* 
* <p>To create an instance, call one of the static methods fromDegrees() or 
* fromRadians().</p> 
* 
* <p>This code was originally published at 
* <a href="http://JanMatuschek.de/LatitudeLongitudeBoundingCoordinates#Java"> 
* http://JanMatuschek.de/LatitudeLongitudeBoundingCoordinates#Java</a>.</p> 
* 
* @author Jan Philip Matuschek 
* @version 27 May 2010 
*/ 
public class GeoLocation { 

    private double radLat; // latitude in radians 
    private double radLon; // longitude in radians 

    private double degLat; // latitude in degrees 
    private double degLon; // longitude in degrees 

    private static final double MIN_LAT = Math.toRadians(-90d); // -PI/2 
    private static final double MAX_LAT = Math.toRadians(90d); // PI/2 
    private static final double MIN_LON = Math.toRadians(-180d); // -PI*2 
    private static final double MAX_LON = Math.toRadians(180d); // PI*2 

    private GeoLocation() { 
    } 

    /** 
    * @param latitude the latitude, in degrees. 
    * @param longitude the longitude, in degrees. 
    */ 
    public static GeoLocation fromDegrees(double latitude, double longitude) { 
     GeoLocation result = new GeoLocation(); 
     result.radLat = Math.toRadians(latitude); 
     result.radLon = Math.toRadians(longitude); 
     result.degLat = latitude; 
     result.degLon = longitude; 
     result.checkBounds(); 
     return result; 
    } 

    /** 
    * @param latitude the latitude, in radians. 
    * @param longitude the longitude, in radians. 
    */ 
    public static GeoLocation fromRadians(double latitude, double longitude) { 
     GeoLocation result = new GeoLocation(); 
     result.radLat = latitude; 
     result.radLon = longitude; 
     result.degLat = Math.toDegrees(latitude); 
     result.degLon = Math.toDegrees(longitude); 
     result.checkBounds(); 
     return result; 
    } 

    private void checkBounds() { 
     if (radLat < MIN_LAT || radLat > MAX_LAT || 
       radLon < MIN_LON || radLon > MAX_LON) 
      throw new IllegalArgumentException(); 
    } 

    /** 
    * @return the latitude, in degrees. 
    */ 
    public double getLatitudeInDegrees() { 
     return degLat; 
    } 

    /** 
    * @return the longitude, in degrees. 
    */ 
    public double getLongitudeInDegrees() { 
     return degLon; 
    } 

    /** 
    * @return the latitude, in radians. 
    */ 
    public double getLatitudeInRadians() { 
     return radLat; 
    } 

    /** 
    * @return the longitude, in radians. 
    */ 
    public double getLongitudeInRadians() { 
     return radLon; 
    } 

    @Override 
    public String toString() { 
     return "(" + degLat + "\u00B0, " + degLon + "\u00B0) = (" + 
       radLat + " rad, " + radLon + " rad)"; 
    } 

    /** 
    * Computes the great circle distance between this GeoLocation instance 
    * and the location argument. 
    * @param radius the radius of the sphere, e.g. the average radius for a 
    * spherical approximation of the figure of the Earth is approximately 
    * 6371.01 kilometers. 
    * @return the distance, measured in the same unit as the radius 
    * argument. 
    */ 
    public double distanceTo(GeoLocation location, double radius) { 
     return Math.acos(Math.sin(radLat) * Math.sin(location.radLat) + 
       Math.cos(radLat) * Math.cos(location.radLat) * 
       Math.cos(radLon - location.radLon)) * radius; 
    } 

    /** 
    * <p>Computes the bounding coordinates of all points on the surface 
    * of a sphere that have a great circle distance to the point represented 
    * by this GeoLocation instance that is less or equal to the distance 
    * argument.</p> 
    * <p>For more information about the formulae used in this method visit 
    * <a href="http://JanMatuschek.de/LatitudeLongitudeBoundingCoordinates"> 
    * http://JanMatuschek.de/LatitudeLongitudeBoundingCoordinates</a>.</p> 
    * @param distance the distance from the point represented by this 
    * GeoLocation instance. Must me measured in the same unit as the radius 
    * argument. 
    * @param radius the radius of the sphere, e.g. the average radius for a 
    * spherical approximation of the figure of the Earth is approximately 
    * 6371.01 kilometers. 
    * @return an array of two GeoLocation objects such that:<ul> 
    * <li>The latitude of any point within the specified distance is greater 
    * or equal to the latitude of the first array element and smaller or 
    * equal to the latitude of the second array element.</li> 
    * <li>If the longitude of the first array element is smaller or equal to 
    * the longitude of the second element, then 
    * the longitude of any point within the specified distance is greater 
    * or equal to the longitude of the first array element and smaller or 
    * equal to the longitude of the second array element.</li> 
    * <li>If the longitude of the first array element is greater than the 
    * longitude of the second element (this is the case if the 180th 
    * meridian is within the distance), then 
    * the longitude of any point within the specified distance is greater 
    * or equal to the longitude of the first array element 
    * <strong>or</strong> smaller or equal to the longitude of the second 
    * array element.</li> 
    * </ul> 
    */ 
    public GeoLocation[] boundingCoordinates(double distance, double radius) { 

     if (radius < 0d || distance < 0d) 
      throw new IllegalArgumentException(); 

     // angular distance in radians on a great circle 
     double radDist = distance/radius; 

     double minLat = radLat - radDist; 
     double maxLat = radLat + radDist; 

     double minLon, maxLon; 
     if (minLat > MIN_LAT && maxLat < MAX_LAT) { 
      double deltaLon = Math.asin(Math.sin(radDist)/
       Math.cos(radLat)); 
      minLon = radLon - deltaLon; 
      if (minLon < MIN_LON) minLon += 2d * Math.PI; 
      maxLon = radLon + deltaLon; 
      if (maxLon > MAX_LON) maxLon -= 2d * Math.PI; 
     } else { 
      // a pole is within the distance 
      minLat = Math.max(minLat, MIN_LAT); 
      maxLat = Math.min(maxLat, MAX_LAT); 
      minLon = MIN_LON; 
      maxLon = MAX_LON; 
     } 

     return new GeoLocation[]{fromRadians(minLat, minLon), 
       fromRadians(maxLat, maxLon)}; 
    } 

} 

उत्तर

14

चुना एक डिफ़ॉल्ट (रेडियंस या डिग्री) और इसके साथ छड़ी:

4

मैं अपने init विधि में एक बूलियन बस भी शामिल होगा। इसके बजाय दो __init__ तरीकों होने के निम्न कार्य करें:

class geoLocation: 
    def __init__(self, lat, long, degrees=True): 
     if degrees: 
      # process as fromDegrees 
      (self._radLat, self._radLong, self._degLat, self._degLong) = self.fromDegrees(lat, long) 
     else: 
      (self._radLat, self._radLong, self._degLat, self._degLong) = self.fromRadians(lat, long) 

    def fromDegrees(self, lat, long): 
     # some function returning radLat and long and degLat and long in a tuple 
    def fromRadians(self, lat, long): 
     # same idea but different calculations 
1

एक अन्य विकल्प GeoLocation की उपवर्गों करने के लिए है, DegreesGeoLocation और RadiansGeoLocation का कहना है। अब आप अपने प्रत्येक इनिट फ़ंक्शन दे सकते हैं।

अब आप रेडियंस का उपयोग करते हुए और डिग्री का उपयोग करते समय अपनी कक्षा में स्थान दो बार संग्रहीत कर रहे हैं। यदि आप गलती से एक प्रतिनिधित्व को संशोधित करते हैं लेकिन दूसरे को भूल जाते हैं तो इससे समस्याएं पैदा हो सकती हैं। मुझे लगता है कि आप एक प्रतिनिधित्व का सबसे अच्छा उपयोग कर सकते हैं, और गेटर्स और सेटर्स प्रदान कर सकते हैं जो अंततः दूसरे प्रतिनिधित्व में रूपांतरण करते हैं।

class geoLocation(object): 
    @classmethod 
    def fromDegrees(cls, lat, long): 
     return cls(lat, long, True) 

    @classmethod 
    def fromDegrees(cls, lat, long): 
     return cls(lat, long, False) 

    def __init__(self, lat, long, degrees=True): 
     if degrees: 
      #blah 
     else: 
      #blah 
2

एक विकल्प कारखाने वर्ग तरीकों का उपयोग करने के लिए है। आप स्वचालित रूप से दूसरे में कनवर्ट करने के लिए क्लासमेड लिख सकते हैं:

class geoLocation: 
    def __init__(self, lat, long): 
     """init class from lat,long as radians""" 

    @classmethod 
    def fromDegrees(cls, dlat, dlong): 
     """creat `cls` from lat,long in degrees """ 
     return cls(to_radians(dlat), to_radians(dlong)) 

    @classmethod 
    def fromRadians(cls, lat, long): # just in case 
     return cls(lat, long) 

obj = geoLocation.fromDegrees(10,20) # returns a new geoLocation object 
संबंधित मुद्दे

 संबंधित मुद्दे