public double distance(Location loc) { double delta_lat, delta_lon; double temp; double lat1 = latitude; double lon1 = longitude; double lat2 = loc.latitude; double lon2 = loc.longitude; // convert degrees to radians lat1 *= RAD_CONVERT; lat2 *= RAD_CONVERT; // find the deltas delta_lat = lat2 - lat1; delta_lon = (lon2 - lon1) * RAD_CONVERT; // Find the great circle distance temp = Math.Pow(Math.Sin(delta_lat/2), 2) + Math.Cos(lat1) * Math.Cos(lat2) * Math.Pow(Math.Sin(delta_lon/2), 2); return EARTH_DIAMETER * Math.Atan2(Math.Sqrt(temp), Math.Sqrt(1-temp)); }
public Location GetLocationV6(IPAddress addr) { byte[] recordBuf = new byte[DbFlags.FULL_RECORD_LENGTH]; char[] recordBuf2 = new char[DbFlags.FULL_RECORD_LENGTH]; int recordBufOffset = 0; Location record = new Location(); int strLength = 0; double latitude = 0, longitude = 0; try { int seekCountry = SeekCountryV6(addr); if (seekCountry == _databaseSegments[0]) { return null; } int recordPointer = seekCountry + ((2 * _recordLength - 1) * _databaseSegments[0]); recordBuf = _dbReader.Read(recordPointer, DbFlags.FULL_RECORD_LENGTH); for (int a0 = 0; a0 < DbFlags.FULL_RECORD_LENGTH; a0++) { recordBuf2[a0] = Convert.ToChar(recordBuf[a0]); } // get country record.CountryCode = CountryConstants.CountryCodes[UnsignedByteToInt(recordBuf[0])]; record.CountryName = CountryConstants.CountryNames[UnsignedByteToInt(recordBuf[0])]; recordBufOffset++; // get region while (recordBuf[recordBufOffset + strLength] != '\0') strLength++; if (strLength > 0) { record.Region = new String(recordBuf2, recordBufOffset, strLength); } recordBufOffset += strLength + 1; strLength = 0; // get region_name record.RegionName = RegionName.GetRegionName(record.CountryCode, record.Region); // get city while (recordBuf[recordBufOffset + strLength] != '\0') strLength++; if (strLength > 0) { record.City = new String(recordBuf2, recordBufOffset, strLength); } recordBufOffset += (strLength + 1); strLength = 0; // get postal code while (recordBuf[recordBufOffset + strLength] != '\0') strLength++; if (strLength > 0) { record.PostalCode = new String(recordBuf2, recordBufOffset, strLength); } recordBufOffset += (strLength + 1); // get latitude int j; for (j = 0; j < 3; j++) latitude += (UnsignedByteToInt(recordBuf[recordBufOffset + j]) << (j * 8)); record.Latitude = (float)latitude / 10000 - 180; recordBufOffset += 3; // get longitude for (j = 0; j < 3; j++) longitude += (UnsignedByteToInt(recordBuf[recordBufOffset + j]) << (j * 8)); record.Longitude = (float)longitude / 10000 - 180; record.MetroCode = record.DmaCode = 0; record.AreaCode = 0; if (_databaseType == DatabaseTypeCodes.CITY_EDITION_REV1 || _databaseType == DatabaseTypeCodes.CITY_EDITION_REV1_V6) { // get metro_code int metroareaCombo = 0; if (record.CountryCode == "US") { recordBufOffset += 3; for (j = 0; j < 3; j++) metroareaCombo += (UnsignedByteToInt(recordBuf[recordBufOffset + j]) << (j * 8)); record.MetroCode = record.DmaCode = metroareaCombo / 1000; record.AreaCode = metroareaCombo % 1000; } } } catch (IOException) { Trace.WriteLine("IO Exception while seting up segments"); } return record; }
public Location getLocationV6(IPAddress addr) { int record_pointer; byte[] record_buf = new byte[FULL_RECORD_LENGTH]; char[] record_buf2 = new char[FULL_RECORD_LENGTH]; int record_buf_offset = 0; Location record = new Location(); int str_length = 0; int j, Seek_country; double latitude = 0, longitude = 0; try { Seek_country = SeekCountryV6(addr); if (Seek_country == databaseSegments[0]) return null; record_pointer = Seek_country + ((2 * recordLength - 1) * databaseSegments[0]); if ((dboptions & GEOIP_MEMORY_CACHE) == 1) Array.Copy(dbbuffer, record_pointer, record_buf, 0, Math.Min(dbbuffer.Length - record_pointer, FULL_RECORD_LENGTH)); else { lock (ioLock) { file.Seek(record_pointer,SeekOrigin.Begin); file.Read(record_buf,0,FULL_RECORD_LENGTH); } } for (int a0 = 0;a0 < FULL_RECORD_LENGTH;a0++) record_buf2[a0] = Convert.ToChar(record_buf[a0]); // get country record.countryCode = countryCode[unsignedByteToInt(record_buf[0])]; record.countryName = countryName[unsignedByteToInt(record_buf[0])]; record_buf_offset++; // get region while (record_buf[record_buf_offset + str_length] != '\0') str_length++; if (str_length > 0) record.region = new String(record_buf2, record_buf_offset, str_length); record_buf_offset += str_length + 1; str_length = 0; // get region_name record.regionName = RegionName.getRegionName( record.countryCode, record.region ); // get city while (record_buf[record_buf_offset + str_length] != '\0') str_length++; if (str_length > 0) record.city = new String(record_buf2, record_buf_offset, str_length); record_buf_offset += (str_length + 1); str_length = 0; // get postal code while (record_buf[record_buf_offset + str_length] != '\0') str_length++; if (str_length > 0) record.postalCode = new String(record_buf2, record_buf_offset, str_length); record_buf_offset += (str_length + 1); // get latitude for (j = 0; j < 3; j++) latitude += (unsignedByteToInt(record_buf[record_buf_offset + j]) << (j * 8)); record.latitude = (float)latitude/10000 - 180; record_buf_offset += 3; // get longitude for (j = 0; j < 3; j++) longitude += (unsignedByteToInt(record_buf[record_buf_offset + j]) << (j * 8)); record.longitude = (float)longitude/10000 - 180; record.metro_code = record.dma_code = 0; record.area_code = 0; if (databaseType == DatabaseInfo.CITY_EDITION_REV1 ||databaseType == DatabaseInfo.CITY_EDITION_REV1_V6) { // get metro_code int metroarea_combo = 0; if (record.countryCode == "US") { record_buf_offset += 3; for (j = 0; j < 3; j++) metroarea_combo += (unsignedByteToInt(record_buf[record_buf_offset + j]) << (j * 8)); record.metro_code = record.dma_code = metroarea_combo/1000; record.area_code = metroarea_combo % 1000; } } } catch (IOException) { Console.WriteLine("IO Exception while seting up segments"); } return record; }