# Path: ncdjango/utils.py
# def proj4_to_epsg(projection):
#     """Attempts to convert a PROJ4 projection object to an EPSG code and returns None if conversion fails"""
# 
#     def make_definition(value):
#         return {x.strip().lower() for x in value.split('+') if x}
# 
#     # Use the EPSG in the definition if available
#     match = EPSG_RE.search(projection.srs)
#     if match:
#         return int(match.group(1))
# 
#     # Otherwise, try to look up the EPSG from the pyproj data file
#     pyproj_data_dir = os.path.join(os.path.dirname(pyproj.__file__), 'data')
#     pyproj_epsg_file = os.path.join(pyproj_data_dir, 'epsg')
#     if os.path.exists(pyproj_epsg_file):
#         definition = make_definition(projection.srs)
#         f = open(pyproj_epsg_file, 'r')
#         for line in f.readlines():
#             match = PYPROJ_EPSG_FILE_RE.search(line)
#             if match:
#                 file_definition = make_definition(match.group(2))
#                 if definition == file_definition:
#                     return int(match.group(1))
#     return None
# 
# def timestamp_to_date(timestamp):
#     return datetime.utcfromtimestamp(0).replace(tzinfo=utc) + timedelta(seconds=timestamp)
# 
# def date_to_timestamp(date_obj):
#     return int((date_obj - datetime.utcfromtimestamp(0).replace(tzinfo=utc)).total_seconds())
# 
# Path: ncdjango/interfaces/arcgis/wkid.py



from datetime import datetime
from django import forms
from django.core.exceptions import ValidationError
from pyproj import Proj
from shapely.geometry import MultiPoint
from shapely.geometry.base import BaseGeometry
from shapely.geometry.multilinestring import MultiLineString
from shapely.geometry.point import Point
from shapely.geometry.polygon import LinearRing, Polygon
from trefoil.geometry.bbox import BBox
from ncdjango.utils import proj4_to_epsg, timestamp_to_date, date_to_timestamp
from .wkid import wkid_to_proj
import json

        epsg = proj4_to_epsg(value)
        if epsg:
            return str(epsg)
        else:
            raise ValidationError('Could not convert projection to EPSG/WKID')


class TimeField(forms.Field):
    """Single timestamp or extent"""

    def to_python(self, value):
        if not value or isinstance(value, (datetime, tuple, list)):
            return value

        try:
            if ',' in value:
                return tuple([timestamp_to_date(int(x) // 1000) for x in value.split(',')])
            else:
                return datetime(int(value))
        except ValueError:
            return None

    def prepare_value(self, value):
        if not value:
            return value

        if isinstance(value, (tuple, list)):
            return ",".join([str(date_to_timestamp(x) * 1000) for x in value])
        else:

# Path: ncdjango/utils.py
# def proj4_to_epsg(projection):
#     """Attempts to convert a PROJ4 projection object to an EPSG code and returns None if conversion fails"""
# 
#     def make_definition(value):
#         return {x.strip().lower() for x in value.split('+') if x}
# 
#     # Use the EPSG in the definition if available
#     match = EPSG_RE.search(projection.srs)
#     if match:
#         return int(match.group(1))
# 
#     # Otherwise, try to look up the EPSG from the pyproj data file
#     pyproj_data_dir = os.path.join(os.path.dirname(pyproj.__file__), 'data')
#     pyproj_epsg_file = os.path.join(pyproj_data_dir, 'epsg')
#     if os.path.exists(pyproj_epsg_file):
#         definition = make_definition(projection.srs)
#         f = open(pyproj_epsg_file, 'r')
#         for line in f.readlines():
#             match = PYPROJ_EPSG_FILE_RE.search(line)
#             if match:
#                 file_definition = make_definition(match.group(2))
#                 if definition == file_definition:
#                     return int(match.group(1))
#     return None
# 
# def timestamp_to_date(timestamp):
#     return datetime.utcfromtimestamp(0).replace(tzinfo=utc) + timedelta(seconds=timestamp)
# 
# def date_to_timestamp(date_obj):
#     return int((date_obj - datetime.utcfromtimestamp(0).replace(tzinfo=utc)).total_seconds())
# 
# Path: ncdjango/interfaces/arcgis/wkid.py



# Path: ncdjango/interfaces/arcgis/form_fields.py
from datetime import datetime
from django import forms
from django.core.exceptions import ValidationError
from pyproj import Proj
from shapely.geometry import MultiPoint
from shapely.geometry.base import BaseGeometry
from shapely.geometry.multilinestring import MultiLineString
from shapely.geometry.point import Point
from shapely.geometry.polygon import LinearRing, Polygon
from trefoil.geometry.bbox import BBox
from ncdjango.utils import proj4_to_epsg, timestamp_to_date, date_to_timestamp
from .wkid import wkid_to_proj
import json


        epsg = proj4_to_epsg(value)
        if epsg:
            return str(epsg)
        else:
            raise ValidationError('Could not convert projection to EPSG/WKID')


class TimeField(forms.Field):
    """Single timestamp or extent"""

    def to_python(self, value):
        if not value or isinstance(value, (datetime, tuple, list)):
            return value

        try:
            if ',' in value:
                return tuple([timestamp_to_date(int(x) // 1000) for x in value.split(',')])
            else:
                return datetime(int(value))
        except ValueError:
            return None

    def prepare_value(self, value):
        if not value:
            return value

        if isinstance(value, (tuple, list)):
            return ",".join([str(date_to_timestamp(x) * 1000) for x in value])
        else:

            return str(date_to_timestamp(value) * 1000)