Beispiel #1
0
        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.Write("IO Exception while seting up segments");
            }
            return record;
        }
Beispiel #2
0
        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));
        }