1"""OGR 64bit handling: https://trac.osgeo.org/gdal/wiki/rfc31_ogr_64
2
3Shapefile: OFTInteger fields are created by default with a width of 9
4characters, so to be unambiguously read as OFTInteger (and if specifying
5integer that require 10 or 11 characters. the field is dynamically extended
6like managed since a few versions). OFTInteger64 fields are created by default
7with a width of 18 digits, so to be unambiguously read as OFTInteger64, and
8extented to 19 or 20 if needed. Integer fields of width between 10 and 18
9will be read as OFTInteger64. Above they will be treated as OFTReal. In
10previous GDAL versions, Integer fields were created with a default with of 10,
11and thus will be now read as OFTInteger64. An open option, DETECT_TYPE=YES, can
12be specified so as OGR does a full scan of the DBF file to see if integer
13fields of size 10 or 11 hold 32 bit or 64 bit values and adjust the type
14accordingly (and same for integer fields of size 19 or 20, in case of overflow
15of 64 bit integer, OFTReal is chosen)
16"""
17
18import pytest
19
20import fiona
21from fiona.env import calc_gdal_version_num, get_gdal_version_num
22
23
24@pytest.mark.xfail(fiona.gdal_version.major < 2,
25                   reason="64-bit integer fields require GDAL 2+")
26def testCreateBigIntSchema(tmpdir):
27    name = str(tmpdir.join('output1.shp'))
28
29    a_bigint = 10 ** 18 - 1
30    fieldname = 'abigint'
31
32    kwargs = {
33        'driver': 'ESRI Shapefile',
34        'crs': 'EPSG:4326',
35        'schema': {
36            'geometry': 'Point',
37            'properties': [(fieldname, 'int:10')]}}
38
39    with fiona.open(name, 'w', **kwargs) as dst:
40        rec = {}
41        rec['geometry'] = {'type': 'Point', 'coordinates': (0, 0)}
42        rec['properties'] = {fieldname: a_bigint}
43        dst.write(rec)
44
45    with fiona.open(name) as src:
46        if fiona.gdal_version >= (2, 0, 0):
47            first = next(iter(src))
48            assert first['properties'][fieldname] == a_bigint
49
50
51@pytest.mark.skipif(get_gdal_version_num() < calc_gdal_version_num(2, 0, 0),
52                    reason="Test requires GDAL 2+")
53@pytest.mark.parametrize('dtype', ['int', 'int64'])
54def test_issue691(tmpdir, dtype):
55    """Type 'int' maps to 'int64'"""
56    schema = {'geometry': 'Any', 'properties': {'foo': dtype}}
57    with fiona.open(
58            str(tmpdir.join('test.shp')), 'w', driver='Shapefile',
59            schema=schema, crs='epsg:4326') as dst:
60        dst.write({
61            'type': 'Feature',
62            'geometry': {'type': 'Point',
63                         'coordinates': (-122.278015, 37.868995)},
64            'properties': {'foo': 3694063472}})
65
66    with fiona.open(str(tmpdir.join('test.shp'))) as src:
67        assert src.schema['properties']['foo'] == 'int:18'
68        first = next(iter(src))
69        assert first['properties']['foo'] == 3694063472
70