# Path: openaddr/sample.py
# def sample_geojson(stream, max_features):
#     ''' Read a stream of input GeoJSON and return a string with a limited feature count.
#     '''
#     features = list()
# 
#     for feature in stream_geojson(stream):
#         if len(features) == max_features:
#             break
# 
#         features.append(feature)
# 
#     geojson = dict(type='FeatureCollection', features=features)
#     return json.dumps(geojson)
# 
# def stream_geojson(stream):
#     '''
#     '''
#     data = ijson.parse(stream)
# 
#     for (prefix1, event1, value1) in data:
#         if event1 != 'start_map':
#             # A root GeoJSON object is a map.
#             raise ValueError((prefix1, event1, value1))
# 
#         for (prefix2, event2, value2) in data:
#             if event2 == 'map_key' and value2 == 'type':
#                 prefix3, event3, value3 = next(data)
# 
#                 if event3 != 'string' and value3 != 'FeatureCollection':
#                     # We only want GeoJSON feature collections
#                     raise ValueError((prefix3, event3, value3))
# 
#             elif event2 == 'map_key' and value2 == 'features':
#                 prefix4, event4, value4 = next(data)
# 
#                 if event4 != 'start_array':
#                     # We only want lists of features here.
#                     raise ValueError((prefix4, event4, value4))
# 
#                 for (prefix5, event5, value5) in data:
#                     if event5 == 'end_array':
#                         break
# 
#                     # let _build_value() handle the feature.
#                     _data = chain([(prefix5, event5, value5)], data)
#                     feature = _build_value(_data)
#                     yield feature


import logging; _L = logging.getLogger('openaddr.conform')
import os
import errno
import tempfile
import mimetypes
import json
import copy
import csv
import re
from zipfile import ZipFile
from locale import getpreferredencoding
from os.path import splitext
from hashlib import sha1
from uuid import uuid4
from .sample import sample_geojson, stream_geojson
from osgeo import ogr, osr, gdal
            out_fieldnames.append(X_FIELDNAME)
            out_fieldnames.append(Y_FIELDNAME)

        # Write the extracted CSV file
        with open(dest_path, 'w', encoding='utf-8') as dest_fp:
            writer = csv.DictWriter(dest_fp, out_fieldnames)
            writer.writeheader()
            # For every row in the source CSV
            row_number = 0
            for source_row in reader:
                row_number += 1
                if len(source_row) != num_fields:
                    _L.debug("Skipping row. Got %d columns, expected %d", len(source_row), num_fields)
                    continue
                try:
                    out_row = row_extract_and_reproject(source_definition, source_row)
                except Exception as e:
                    _L.error('Error in row {}: {}'.format(row_number, e))
                    raise
                else:
                    writer.writerow(out_row)

def geojson_source_to_csv(source_path, dest_path):
    '''
    '''
    # For every row in the source GeoJSON
    with open(source_path) as file:
        # Write the extracted CSV file
        with open(dest_path, 'w', encoding='utf-8') as dest_fp:
            writer = None

# Path: openaddr/sample.py
# def sample_geojson(stream, max_features):
#     ''' Read a stream of input GeoJSON and return a string with a limited feature count.
#     '''
#     features = list()
# 
#     for feature in stream_geojson(stream):
#         if len(features) == max_features:
#             break
# 
#         features.append(feature)
# 
#     geojson = dict(type='FeatureCollection', features=features)
#     return json.dumps(geojson)
# 
# def stream_geojson(stream):
#     '''
#     '''
#     data = ijson.parse(stream)
# 
#     for (prefix1, event1, value1) in data:
#         if event1 != 'start_map':
#             # A root GeoJSON object is a map.
#             raise ValueError((prefix1, event1, value1))
# 
#         for (prefix2, event2, value2) in data:
#             if event2 == 'map_key' and value2 == 'type':
#                 prefix3, event3, value3 = next(data)
# 
#                 if event3 != 'string' and value3 != 'FeatureCollection':
#                     # We only want GeoJSON feature collections
#                     raise ValueError((prefix3, event3, value3))
# 
#             elif event2 == 'map_key' and value2 == 'features':
#                 prefix4, event4, value4 = next(data)
# 
#                 if event4 != 'start_array':
#                     # We only want lists of features here.
#                     raise ValueError((prefix4, event4, value4))
# 
#                 for (prefix5, event5, value5) in data:
#                     if event5 == 'end_array':
#                         break
# 
#                     # let _build_value() handle the feature.
#                     _data = chain([(prefix5, event5, value5)], data)
#                     feature = _build_value(_data)
#                     yield feature


# Path: openaddr/conform.py
import logging; _L = logging.getLogger('openaddr.conform')
import os
import errno
import tempfile
import mimetypes
import json
import copy
import csv
import re
from zipfile import ZipFile
from locale import getpreferredencoding
from os.path import splitext
from hashlib import sha1
from uuid import uuid4
from .sample import sample_geojson, stream_geojson
from osgeo import ogr, osr, gdal

            out_fieldnames.append(X_FIELDNAME)
            out_fieldnames.append(Y_FIELDNAME)

        # Write the extracted CSV file
        with open(dest_path, 'w', encoding='utf-8') as dest_fp:
            writer = csv.DictWriter(dest_fp, out_fieldnames)
            writer.writeheader()
            # For every row in the source CSV
            row_number = 0
            for source_row in reader:
                row_number += 1
                if len(source_row) != num_fields:
                    _L.debug("Skipping row. Got %d columns, expected %d", len(source_row), num_fields)
                    continue
                try:
                    out_row = row_extract_and_reproject(source_definition, source_row)
                except Exception as e:
                    _L.error('Error in row {}: {}'.format(row_number, e))
                    raise
                else:
                    writer.writerow(out_row)

def geojson_source_to_csv(source_path, dest_path):
    '''
    '''
    # For every row in the source GeoJSON
    with open(source_path) as file:
        # Write the extracted CSV file
        with open(dest_path, 'w', encoding='utf-8') as dest_fp:
            writer = None

            for (row_number, feature) in enumerate(stream_geojson(file)):