ffi-gdal 1.0.0.beta1 → 1.0.0.beta3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/History.md +32 -0
- data/LICENSE.txt +1 -1
- data/TODO.md +0 -2
- data/examples/extract_and_colorize.rb +81 -0
- data/{testing_ogr.rb → examples/geometries.rb} +4 -16
- data/{testing_layer_to_layer.rb → examples/ogr_layer_to_layer.rb} +1 -1
- data/examples/testing_gdal.rb +46 -0
- data/lib/ext/error_symbols.rb +1 -0
- data/lib/ffi/cpl/conv_h.rb +15 -15
- data/lib/ffi/cpl/error_h.rb +13 -13
- data/lib/ffi/cpl/string_h.rb +10 -10
- data/lib/ffi/cpl/vsi_h.rb +23 -23
- data/lib/ffi/gdal.rb +1 -1
- data/lib/ffi/gdal/grid_h.rb +3 -3
- data/lib/ffi/gdal/version.rb +1 -1
- data/lib/ffi/ogr/api_h.rb +3 -3
- data/lib/gdal/dataset.rb +5 -4
- data/lib/gdal/dataset_extensions.rb +2 -2
- data/lib/gdal/raster_band.rb +1 -0
- data/lib/gdal/raster_band_extensions.rb +5 -5
- data/lib/ogr/geometry.rb +1 -0
- data/spec/unit/gdal/color_table_extensions_spec.rb +60 -0
- data/spec/unit/gdal/data_type_spec.rb +79 -0
- data/spec/unit/gdal/dataset_spec.rb +98 -0
- metadata +12 -11
- data/linkies.rb +0 -35
- data/readie.rb +0 -90
- data/testing_gdal.rb +0 -168
- data/testing_gdalwarp.rb +0 -91
- data/testing_ndvi.rb +0 -76
- data/testing_nir.rb +0 -77
- data/things.rb +0 -84
data/readie.rb
DELETED
@@ -1,90 +0,0 @@
|
|
1
|
-
require './lib/ffi-gdal'
|
2
|
-
require 'narray'
|
3
|
-
|
4
|
-
|
5
|
-
#dir = '../../agrian/gis_engine/test/test_files'
|
6
|
-
#name = 'empty_red_image.tif'
|
7
|
-
#name = 'empty_black_image.tif'
|
8
|
-
|
9
|
-
#dir = '~/Desktop/geotiffs'
|
10
|
-
#name = 'NDVI20000201032.tif'
|
11
|
-
#name = 'NDVI20000701183.tif'
|
12
|
-
#name = 'NDVI20000701183.zip'
|
13
|
-
#name = 'NDVI20000401092.tif'
|
14
|
-
|
15
|
-
#dir = './spec/support'
|
16
|
-
#name = 'google_earth_test.jpg'
|
17
|
-
#name = 'compassdata_gcparchive_google_earth.kmz'
|
18
|
-
|
19
|
-
#dir = './spec/support/aaron/Floyd'
|
20
|
-
#name = 'Floyd_1058_20140612_NRGB.tif'
|
21
|
-
#name = 'Floyd_1058_20140612_RGBNRGB.bmp'
|
22
|
-
#name = 'Floyd_1058_20140612_RGBNRGB.jpg'
|
23
|
-
|
24
|
-
dir = './spec/support/images/Harper'
|
25
|
-
name = 'Harper_1058_20140612_NRGB.tif'
|
26
|
-
|
27
|
-
#dir = './spec/support/osgeo'
|
28
|
-
#name = 'c41078a1.tif'
|
29
|
-
|
30
|
-
filename = File.expand_path(name, dir)
|
31
|
-
dataset = GDAL::Dataset.open(filename, 'r')
|
32
|
-
|
33
|
-
abort('No raster bands') if dataset.raster_count == 0
|
34
|
-
GDAL.log "raster count: #{dataset.raster_count}"
|
35
|
-
|
36
|
-
def raster_stuff(band)
|
37
|
-
GDAL.log "x size: #{band.x_size}"
|
38
|
-
GDAL.log "y size: #{band.y_size}"
|
39
|
-
GDAL.log "min: #{band.minimum_value}"
|
40
|
-
GDAL.log "max: #{band.maximum_value}"
|
41
|
-
GDAL.log "block size: #{band.block_size}"
|
42
|
-
GDAL.log "color interp: #{band.color_interpretation}"
|
43
|
-
lines = []
|
44
|
-
|
45
|
-
band.readlines do |line|
|
46
|
-
lines << line
|
47
|
-
end
|
48
|
-
|
49
|
-
GDAL.log "line height: #{lines.size}"
|
50
|
-
lines
|
51
|
-
end
|
52
|
-
|
53
|
-
bands = []
|
54
|
-
|
55
|
-
|
56
|
-
1.upto(dataset.raster_count) do |i|
|
57
|
-
GDAL.log "Checking band #{i}"
|
58
|
-
band = dataset.raster_band(i)
|
59
|
-
GDAL.log "* color interp: #{band.color_interpretation}"
|
60
|
-
|
61
|
-
if %i[GCI_RedBand GCI_Undefined].include? band.color_interpretation
|
62
|
-
bands << {
|
63
|
-
band: band,
|
64
|
-
array: NArray.to_na(raster_stuff(dataset.raster_band(i)))
|
65
|
-
}
|
66
|
-
end
|
67
|
-
end
|
68
|
-
|
69
|
-
red = bands.find { |hash| hash[:band].color_interpretation == :GCI_RedBand }
|
70
|
-
nir = bands.find { |hash| hash[:band].color_interpretation == :GCI_Undefined }
|
71
|
-
|
72
|
-
if nir.nil?
|
73
|
-
abort 'No near-infrared band found!'
|
74
|
-
end
|
75
|
-
ndvi = (nir[:array] - red[:array]) / (nir[:array] + red[:array])
|
76
|
-
|
77
|
-
GDAL.log "NDVI array created"
|
78
|
-
|
79
|
-
GDAL.log dataset.driver.long_name
|
80
|
-
GDAL.log dataset.driver.all_metadata[:DEFAULT]["DCAP_CREATE"]
|
81
|
-
|
82
|
-
dataset.driver.create_dataset('testie.tif', ndvi.sizes.first, ndvi.sizes.last) do |out_dataset|
|
83
|
-
GDAL.log "raster count: #{out_dataset.raster_count}"
|
84
|
-
out_band = out_dataset.raster_band(1)
|
85
|
-
out_band.write_array(ndvi)
|
86
|
-
|
87
|
-
# need to add metadata and raster data
|
88
|
-
# http://www.gdal.org/gdal_tutorial.html
|
89
|
-
end
|
90
|
-
|
data/testing_gdal.rb
DELETED
@@ -1,168 +0,0 @@
|
|
1
|
-
require 'RMagick'
|
2
|
-
require 'bundler/setup'
|
3
|
-
require 'pry'
|
4
|
-
require 'ffi-gdal'
|
5
|
-
|
6
|
-
GDAL::Logger.logging_enabled = true
|
7
|
-
|
8
|
-
#proj = 'MULTIPOLYGON (((-120.03296317083633 44.226828996384285, -120.03199741570855 44.230616638863914, -120.03305119285443 44.23322276021398, -120.03305439612939 44.23323503554639, -120.03305423080232 44.233247523039175, -120.03305070309673 44.23325975261691, -120.03304394580853 44.233271263913274, -120.03303421330706 44.233281623601044, -120.0330218719597 44.23329044170392, -120.03300738634044 44.23329738627662, -120.03299130174162 44.23330219590042, -120.03297422364709 44.23330468952384, -120.03295679493954 44.23330477327791, -120.0329396717 44.23330244400986, -120.03259652218235 44.23323046886841, -120.03259605091168 44.23323469214073, -120.03259130838445 44.233246748969734, -120.03258339633375 44.23325791928269, -120.03257261443646 44.233267779993575, -120.03255937106756 44.233275957618844, -120.03254416783288 44.23328214242357, -120.03252758057009 44.23328610015274, -120.03251023753826 44.2332876809039, -120.032492795622 44.23328682480475, -120.0324759154512 44.23328356428076, -120.03246023637918 44.23327802282714, -120.03244635226639 44.233270410331386, -120.03229057481504 44.233166296752856, -120.03144275436163 44.23298846700243, -120.03135705202111 44.23542539118272, -120.03135483571377 44.23543806847976, -120.0313491680669 44.2354501823413, -120.0313402724131 44.23546125542315, -120.03132849928409 44.23547085139311, -120.03131431259804 44.235478592124444, -120.03129827137883 44.235484172595754, -120.03128100772805 44.23548737291011, -120.03126320191683 44.23548806696012, -120.03124555558011 44.235486227396926, -120.03122876406881 44.235481926707976, -120.03121348904955 44.23547533436071, -120.03120033243181 44.23546671012462, -120.03118981264993 44.23545639383555, -120.02629194435607 44.229488174527326, -120.0262841759258 44.22947593881562, -120.0262801234077 44.229462813795166, -120.02627996353844 44.22944937186798, -120.0262837032901 44.22943619925734, -120.02629117956641 44.22942387044152, -120.02630206631528 44.22941292309999, -120.02631588874878 44.229403834664076, -120.02650834059244 44.22930162063937, -120.02596466290531 44.22893822933382, -120.02595288197551 44.22892861642798, -120.02594398682248 44.22891752390089, -120.02593832881524 44.2289053899198, -120.02593613145164 44.22889269379079, -120.02593748153026 44.22887993702566, -120.02594232572132 44.2288676235318, -120.02595047267361 44.22885623970722, -120.0259616005729 44.228846235226946, -120.02597526985406 44.22883800528063, -120.0259909405644 44.22883187496161, -120.02600799369236 44.22882808642537, -120.02602575561929 44.228826789324046, -120.02604352472815 44.22882803489487, -120.02606059911827 44.2288317739361, -120.02607630433104 44.22883785875077, -120.02675839864344 44.229168810839475, -120.02688733695234 44.22910032953997, -120.02611125484312 44.228061533840545, -120.02610457348571 44.22805003875662, -120.02610110582951 44.22803783693943, -120.02610098163969 44.228025384999874, -120.02610420556363 44.2280131489091, -120.02611065695699 44.22800158656121, -120.0261200943985 44.22799113063793, -120.02613216472427 44.22798217241707, -120.02614641624366 44.227975047130094, -120.0261623156424 44.22797002141719, -120.02617926794002 44.22796728334896, -120.0261966387548 44.227966935388565, -120.02621377804353 44.22796899055725, -120.02623004442708 44.22797337194719, -120.0262448291917 44.2279799155993, -120.02625757906833 44.22798837663918, -120.02626781693645 44.22799843844071, -120.02754662923428 44.229551257921585, -120.030836241635 44.23114734101261, -120.03136956622194 44.229959117652726, -120.03138391393463 44.22955111324294, -120.03138620312208 44.229538207384174, -120.0313920671636 44.22952589494864, -120.03140126651873 44.229514678887824, -120.03141342540293 44.229505017367465, -120.03142804713777 44.22949730505196, -120.03144453443974 44.229491856982676, -120.03146221381887 44.22948889570851, -120.03148036308987 44.22948854219496, -120.03149824087296 44.229490810882744, -120.03151511687828 44.22949560909792, -120.03153030173767 44.22950274083756, -120.03154317516459 44.22951191477631, -120.0315532112922 44.22952275616695, -120.03156000257852 44.22953482814357, -120.03278854263438 44.22679758476189, -120.03286127379035 44.226512326846944, -120.03286465252121 44.226503048718484, -120.03286991708133 44.22649423317623, -120.03301731535839 44.22629080564284, -120.03302703028369 44.22628013425537, -120.03303948802117 44.22627104306898, -120.03305420051213 44.22626388825138, -120.03307059136289 44.22625895010873, -120.0330880184265 44.22625642210408, -120.03310579896007 44.22625640327766, -120.03312323637284 44.226258894367135, -120.03313964751646 44.22626379777834, -120.03315438944897 44.22627092140904, -120.03316688462326 44.22627998617475, -120.03317664351395 44.22629063694302, -120.03318328379541 44.22630245644617, -120.03318654532022 44.226314981629145, -120.03318630031114 44.22632772179051, -120.0331825583669 44.22634017780693, -120.03296317083633 44.226828996384285)))'
|
9
|
-
|
10
|
-
floyd_too_big_wkt = 'MULTIPOLYGON (((-87.55634718933241 31.168633650404765, -87.552227316286 31.16870709121005, -87.55234533348232 31.169808696448463, -87.5478606800096 31.1698913163249, -87.54777484932141 31.168679550914895, -87.54380517997858 31.168615290194918, -87.54396611251944 31.16511760526154, -87.55647593536513 31.164906454793982, -87.55634718933241 31.168633650404765)))'
|
11
|
-
floyd_wkt = 'MULTIPOLYGON (((-87.5530099868775 31.16710573359053,-87.5530099868775 31.165600160261103,-87.55384683609009 31.16710573359053,-87.5530099868775 31.16710573359053)))'
|
12
|
-
floyd_srid = 4326
|
13
|
-
|
14
|
-
harper_path = '/Users/sloveless/Development/projects/ffi-gdal/spec/support/images/Harper/Harper_1058_20140612_NRGB.tif'
|
15
|
-
harper = GDAL::Dataset.open(harper_path, 'r')
|
16
|
-
|
17
|
-
floyd_path = '/Users/sloveless/Development/projects/ffi-gdal/spec/support/images/Floyd/Floyd_1058_20140612_NRGB.tif'
|
18
|
-
floyd = GDAL::Dataset.open(floyd_path, 'r')
|
19
|
-
|
20
|
-
spatial_ref = OGR::SpatialReference.new(floyd.projection)
|
21
|
-
floyd_geometry = OGR::Geometry.create_from_wkt(floyd_wkt, spatial_ref)
|
22
|
-
|
23
|
-
usg_path = '/Users/sloveless/Development/projects/ffi-gdal/spec/support/images/osgeo/geotiff/usgs/c41078a1.tif'
|
24
|
-
usg = GDAL::Dataset.open(usg_path, 'r')
|
25
|
-
|
26
|
-
world_file_path = "#{__dir__}/spec/support/worldfiles/SR_50M/SR_50M.tif"
|
27
|
-
world_file = GDAL::GeoTransform.from_world_file(world_file_path, 'tfw')
|
28
|
-
|
29
|
-
binding.pry
|
30
|
-
|
31
|
-
def extract_ndvi(dataset, destination_path, wkt_geometry)
|
32
|
-
nir_band = dataset.raster_band(1)
|
33
|
-
red_band = dataset.raster_band(2)
|
34
|
-
nir_array = nir_band.to_na
|
35
|
-
red_array = red_band.to_na
|
36
|
-
|
37
|
-
# Create an OGR::Geometry from the WKT and convert to dataset's projection.
|
38
|
-
wkt_spatial_ref = OGR::SpatialReference.new_from_epsg(4326)
|
39
|
-
geometry = OGR::Geometry.create_from_wkt(wkt_geometry,
|
40
|
-
wkt_spatial_ref)
|
41
|
-
geometry.transform_to!(dataset.spatial_reference)
|
42
|
-
|
43
|
-
# Create the geo_tranform for the geometry-raster, based on the original
|
44
|
-
# dataset's geo_transform.
|
45
|
-
pixel_extent = geometry.envelope.world_to_pixel(dataset.geo_transform)
|
46
|
-
p pixel_extent
|
47
|
-
pixel_height = pixel_extent[:x_max]
|
48
|
-
pixel_width = pixel_extent[:y_max]
|
49
|
-
|
50
|
-
y_range = pixel_extent[:y_origin]..(pixel_extent[:y_max] - 1)
|
51
|
-
x_range = pixel_extent[:x_origin]...(pixel_extent[:x_max] - 1)
|
52
|
-
red_clip_array = red_array[x_range, y_range]
|
53
|
-
nir_clip_array = nir_array[x_range, y_range]
|
54
|
-
|
55
|
-
|
56
|
-
#geo = GDAL::GeoTransform.new
|
57
|
-
geo = dataset.geo_transform
|
58
|
-
#geo.x_origin = dataset.geo_transform.x_origin + pixel_extent[:x_origin]
|
59
|
-
geo.x_origin = pixel_extent[:x_origin]
|
60
|
-
geo.pixel_width = pixel_extent[:pixel_width]
|
61
|
-
geo.x_rotation = 0
|
62
|
-
|
63
|
-
#geo.y_origin = dataset.geo_transform.y_origin + pixel_extent[:y_origin]
|
64
|
-
#geo.y_origin = pixel_extent[:y_origin]
|
65
|
-
geo.y_origin = pixel_extent[:y_max].abs # the book shows this--is it right?
|
66
|
-
geo.pixel_height = pixel_extent[:pixel_height]
|
67
|
-
geo.y_rotation = 0
|
68
|
-
|
69
|
-
# Get pixels for each point of each linear ring in the geometry.
|
70
|
-
pixels = case geometry.type
|
71
|
-
when :wkbMultiPolygon
|
72
|
-
polygon = geometry.geometry_at(0)
|
73
|
-
linear_ring = polygon.geometry_at(0)
|
74
|
-
#linear_ring.pixels(dataset.geo_transform)
|
75
|
-
linear_ring.pixels(geo)
|
76
|
-
else
|
77
|
-
raise 'bleh'
|
78
|
-
end
|
79
|
-
|
80
|
-
|
81
|
-
# Write the pixels to an array at the size of the new image
|
82
|
-
image = Magick::Image.new(pixel_extent[:x_max], pixel_extent[:y_max])
|
83
|
-
poly = Magick::Draw.new
|
84
|
-
poly.fill('#00ffff')
|
85
|
-
poly.polygon(*pixels)
|
86
|
-
poly.draw(image)
|
87
|
-
image.write('bobo.tif')
|
88
|
-
|
89
|
-
excerpted_image = image.excerpt(pixel_extent[:x_origin], pixel_extent[:y_origin],
|
90
|
-
pixel_extent[:pixel_count], pixel_extent[:line_count])
|
91
|
-
# rasterized_pixels = excerpted_image.export_pixels(0, 0,
|
92
|
-
# pixel_extent[:pixel_count], pixel_extent[:line_count], 'I')
|
93
|
-
rasterized_pixels = image.export_pixels(pixel_extent[:x_origin], pixel_extent[:y_origin],
|
94
|
-
pixel_extent[:pixel_count], pixel_extent[:line_count], 'I')
|
95
|
-
mask = NArray[rasterized_pixels]
|
96
|
-
mask.reshape!(pixel_extent[:pixel_count], pixel_extent[:line_count])
|
97
|
-
p mask.to_a.flatten.count { |b| !b.zero? }
|
98
|
-
p red_clip_array.to_a.flatten.count { |b| !b.zero? }
|
99
|
-
p nir_clip_array.to_a.flatten.count { |b| !b.zero? }
|
100
|
-
|
101
|
-
|
102
|
-
i = 0
|
103
|
-
red_clip_array.each do |_|
|
104
|
-
if mask[i] == 65535
|
105
|
-
red_clip_array[i] = 1.0
|
106
|
-
end
|
107
|
-
|
108
|
-
i += 1
|
109
|
-
end
|
110
|
-
|
111
|
-
i = 0
|
112
|
-
nir_clip_array.each do |_|
|
113
|
-
if mask[i] == 65535
|
114
|
-
nir_clip_array[i] = 1.0
|
115
|
-
end
|
116
|
-
|
117
|
-
i += 1
|
118
|
-
end
|
119
|
-
|
120
|
-
p mask.flatten.to_a.count { |b| !b.zero? }
|
121
|
-
p red_clip_array.to_a.flatten.count { |b| !b.zero? }
|
122
|
-
p nir_clip_array.to_a.flatten.count { |b| !b.zero? }
|
123
|
-
|
124
|
-
binding.pry
|
125
|
-
#ndvi_array = 1.0 * (nir_array - red_array) / nir_array + red_array + 1.0
|
126
|
-
ndvi_array = 1.0 * (nir_clip_array - red_clip_array) / (nir_clip_array + red_clip_array) + 1.0
|
127
|
-
#ndvi_array = (nir_clip_array - red_clip_array) / (nir_clip_array + red_clip_array)
|
128
|
-
|
129
|
-
driver = GDAL::Driver.by_name('GTiff')
|
130
|
-
ndvi_dataset = driver.create_dataset('ndvi.tif', pixel_extent[:x_max], pixel_extent[:y_max]) do |ndvi|
|
131
|
-
ndvi.projection = dataset.projection
|
132
|
-
ndvi.geo_transform = geo
|
133
|
-
|
134
|
-
ndvi_band = ndvi.raster_band(1)
|
135
|
-
# tmp_band.write_array(mask, x_offset: pixel_extent[:x_origin].abs,
|
136
|
-
# y_offset: pixel_extent[:y_origin].abs, data_type: :GDT_Float32)
|
137
|
-
#ndvi_band.write_array(ndvi_array, data_type: :GDT_Float32)
|
138
|
-
ndvi_band.write_array(ndvi_array)
|
139
|
-
#ndvi_band.no_data_value = -9999
|
140
|
-
end
|
141
|
-
|
142
|
-
ndvi_dataset.close
|
143
|
-
|
144
|
-
# Create new dataset from the geometry
|
145
|
-
#tmp_dataset.rasterize_geometries!(1, linear_ring, 1000)
|
146
|
-
|
147
|
-
#binding.pry
|
148
|
-
end
|
149
|
-
|
150
|
-
|
151
|
-
#floyd.image_warp('meow.tif', 'GTiff', 1, cutline: floyd_geometry )
|
152
|
-
#extract_ndvi(floyd, 'ndvi.tif', floyd_wkt)
|
153
|
-
|
154
|
-
def warp_to_geometry(dataset, wkt_geometry)
|
155
|
-
|
156
|
-
# Create an OGR::Geometry from the WKT and convert to dataset's projection.
|
157
|
-
wkt_spatial_ref = OGR::SpatialReference.new_from_epsg(4326)
|
158
|
-
geometry = OGR::Geometry.create_from_wkt(wkt_geometry, wkt_spatial_ref)
|
159
|
-
geometry.transform_to!(dataset.spatial_reference)
|
160
|
-
|
161
|
-
binding.pry
|
162
|
-
# Create a .shp from the geometry
|
163
|
-
shape = geometry.to_vector('geom.shp', 'ESRI Shapefile',
|
164
|
-
spatial_reference: dataset.spatial_reference)
|
165
|
-
shape.close
|
166
|
-
end
|
167
|
-
|
168
|
-
#warp_to_geometry(floyd, floyd_wkt)
|
data/testing_gdalwarp.rb
DELETED
@@ -1,91 +0,0 @@
|
|
1
|
-
require 'bundler/setup'
|
2
|
-
require 'pry'
|
3
|
-
require 'ffi-gdal'
|
4
|
-
|
5
|
-
include GDAL::Logger
|
6
|
-
GDAL::Logger.logging_enabled = true
|
7
|
-
|
8
|
-
|
9
|
-
# https://trac.osgeo.org/gdal/ticket/1599
|
10
|
-
|
11
|
-
#floyd_path = '/Users/sloveless/Development/projects/ffi-gdal/spec/support/images/Floyd/Floyd_1058_20140612_NRGB.tif'
|
12
|
-
#floyd = GDAL::Dataset.open(floyd_path, 'r')
|
13
|
-
#floyd_wkt = 'MULTIPOLYGON(((-87.5530099868775 31.16710573359053,-87.65300998687 31.167105000001, -87.5530099868775 31.165600160261103,-87.55384683609009 31.16710573359053,-87.5530099868775 31.16710573359053)))'
|
14
|
-
spatial_ref = OGR::SpatialReference.new_from_epsg(4326)
|
15
|
-
#floyd_geometry = OGR::Geometry.create_from_wkt(floyd_wkt, spatial_ref)
|
16
|
-
#floyd_geometry.transform_to!(floyd.spatial_reference)
|
17
|
-
|
18
|
-
harper_wkt = "MULTIPOLYGON (((-87.5639814967 31.18677438740807, -87.56095596493151 31.186756030714633, -87.56127783001327 31.185048842709264, -87.56022640407895 31.184259487308854, -87.55996891201355 31.183800556723995, -87.56037660778345 31.183470125325623, -87.55999036968537 31.182295248791167, -87.55936809719456 31.181891381615166, -87.55887457073558 31.18031261157701, -87.55968996227632 31.180018884013712, -87.55872436703105 31.17799948235227, -87.55769439876855 31.177852615097343, -87.55664297283512 31.177283502330614, -87.5565464133106 31.175998396405248, -87.56492563427345 31.176016755184154, -87.56496854961708 31.177154992523167, -87.5639814967 31.177669030707264, -87.56415315807725 31.178752173461035, -87.56501146496161 31.179908735942732, -87.56496854961708 31.18462663017465, -87.56400295437183 31.184810201073287, -87.5639814967 31.18677438740807)))"
|
19
|
-
harper_path = '/Users/sloveless/Development/projects/ffi-gdal/spec/support/images/Harper/Harper_1058_20140612_NRGB.tif'
|
20
|
-
harper = GDAL::Dataset.open(harper_path, 'r')
|
21
|
-
harper_geometry = OGR::Geometry.create_from_wkt(harper_wkt, spatial_ref)
|
22
|
-
harper_geometry.transform_to!(harper.spatial_reference)
|
23
|
-
|
24
|
-
#epsg4326_geom = floyd.to_geometry
|
25
|
-
|
26
|
-
#floyd.close
|
27
|
-
harper.close
|
28
|
-
|
29
|
-
|
30
|
-
#puts floyd.contains_geometry?(floyd_wkt)
|
31
|
-
|
32
|
-
#`gdalwarp -wo "INIT_DEST=255,0,255" -wo "#{floyd_geometry.to_wkt}" #{floyd_path} cut-out.tif`
|
33
|
-
#`gdalwarp -wo "CUTLINE=#{floyd_geometry.to_wkt}" #{floyd_path} cut-out.tif`
|
34
|
-
#`gdalwarp -dstnodata -1 -cutline "#{floyd_geometry.to_wkt}" #{floyd_path} cut-out.tif`
|
35
|
-
#cmd = %{gdalwarp -dstnodata -1 -cutline "#{floyd_geometry.to_wkt}" -crop_to_cutline #{floyd_path} cut-out.tif}
|
36
|
-
#cmd = %{gdalwarp -dstnodata -1 -cutline "#{floyd_geometry.to_wkt}" -crop_to_cutline #{floyd_path} cut-out.tif}
|
37
|
-
|
38
|
-
# Result: No file
|
39
|
-
# cmd = %{gdalwarp -cutline "#{floyd_geometry.to_wkt}" -crop_to_cutline #{floyd_path} cut-out.tif}
|
40
|
-
|
41
|
-
#cmd = %{gdalwarp -wo "INIT_DEST=NO_DATA" -wo "CUTLINE=#{floyd_geometry.to_wkt}" -crop_to_cutline #{floyd_path} cut-out.tif}
|
42
|
-
#cmd = %{gdalwarp -wo "CUTLINE_ALL_TOUCHED=TRUE" -wo "INIT_DEST=NO_DATA" -wo "CUTLINE=#{floyd_geometry.to_wkt}" -crop_to_cutline #{floyd_path} cut-out.tif}
|
43
|
-
#cmd = %{gdalwarp -wo "CUTLINE_ALL_TOUCHED=TRUE" -wo "CUTLINE=#{floyd_geometry.to_wkt}" -wo "SAMPLE_GRID=YES" #{floyd_path} cut-out.tif}
|
44
|
-
#cmd = %{gdalwarp -wo "ALL_TOUCHED=TRUE" -wo "CUTLINE=#{floyd_geometry.to_wkt}" #{floyd_path} cut-out.tif}
|
45
|
-
#cmd = %{gdal_grid -clipsrc "#{floyd_geometry.to_wkt}" #{floyd_path} cut-out.tif}
|
46
|
-
|
47
|
-
require 'fileutils'
|
48
|
-
FileUtils.rm_rf 'stuff'
|
49
|
-
FileUtils.rm_rf 'stuff.shp'
|
50
|
-
FileUtils.rm_rf 'stuff.*'
|
51
|
-
FileUtils.rm_rf 'geometry.json'
|
52
|
-
FileUtils.rm_rf 'cut-out.tif'
|
53
|
-
|
54
|
-
#File.write('geometry.json', harper_geometry.to_geo_json)
|
55
|
-
#File.write('geometry.json', epsg4326_geom.to_geo_json)
|
56
|
-
#ogr_cmd = %{ogr2ogr -f "ESRI Shapefile" stuff.shp geometry.json OGRGeoJSON}
|
57
|
-
#p ogr_cmd
|
58
|
-
|
59
|
-
#{}`#{ogr_cmd}`
|
60
|
-
|
61
|
-
def make_shapefile(geo_json)
|
62
|
-
geo_json_file = Tempfile.new(%w[geo_json .json])
|
63
|
-
geo_json_file.write(geo_json)
|
64
|
-
geo_json_file.close
|
65
|
-
|
66
|
-
shape_dir = Dir.mktmpdir('shapes')
|
67
|
-
shapefile_name = 'clip_shape.shp'
|
68
|
-
shapefile_path = File.join(shape_dir, shapefile_name)
|
69
|
-
|
70
|
-
ogr_command = %{ogr2ogr -f "ESRI Shapefile" #{shapefile_path} #{geo_json_file.path} OGRGeoJSON}
|
71
|
-
logger.debug "Running command to make shapefile: #{ogr_command}"
|
72
|
-
|
73
|
-
`#{ogr_command}`
|
74
|
-
|
75
|
-
FileUtils.rm("#{shape_dir}/clip_shape.prj")
|
76
|
-
|
77
|
-
shape_dir
|
78
|
-
end
|
79
|
-
|
80
|
-
# Result: Empty file
|
81
|
-
#cmd = %{gdalwarp -wo "CUTLINE=#{floyd_wkt}" -s_srs "EPSG:32616" #{floyd_path} cut-out.tif}
|
82
|
-
#prj_file = File.expand_path('stuff.prj', __dir__)
|
83
|
-
#p prj_file
|
84
|
-
#FileUtils.rm prj_file
|
85
|
-
binding.pry
|
86
|
-
|
87
|
-
#cmd = %{gdalwarp -dstnodata -1 -s_srs EPSG:32616 -t_srs EPSG:26916 -crop_to_cutline -cutline stuff.shp #{floyd_path} cut-out.tif}
|
88
|
-
cmd = %{gdalwarp -dstnodata -1 -s_srs EPSG:32616 -t_srs EPSG:26916 -crop_to_cutline -cutline stuff.shp #{harper_path} cut-out.tif}
|
89
|
-
|
90
|
-
p cmd
|
91
|
-
`#{cmd}`
|
data/testing_ndvi.rb
DELETED
@@ -1,76 +0,0 @@
|
|
1
|
-
require 'bundler/setup'
|
2
|
-
require 'pry'
|
3
|
-
require 'ffi-gdal'
|
4
|
-
|
5
|
-
include GDAL::Logger
|
6
|
-
GDAL::Logger.logging_enabled = true
|
7
|
-
|
8
|
-
|
9
|
-
floyd_path = '/Users/sloveless/Development/projects/ffi-gdal/spec/support/images/Floyd/Floyd_1058_20140612_NRGB.tif'
|
10
|
-
floyd = GDAL::Dataset.open(floyd_path, 'r')
|
11
|
-
floyd_wkt = 'MULTIPOLYGON (((-87.5530099868775 31.16710573359053,-87.5530099868775 31.165600160261103,-87.55384683609009 31.16710573359053,-87.5530099868775 31.16710573359053)))'
|
12
|
-
|
13
|
-
# http://pcjericks.github.io/py-gdalogr-cookbook/vector_layers.html#convert-vector-layer-to-array
|
14
|
-
pixel_size = 25
|
15
|
-
no_data_value = -1
|
16
|
-
|
17
|
-
# Get the extent of the geometry
|
18
|
-
spatial_reference = OGR::SpatialReference.new_from_epsg(4326)
|
19
|
-
geometry = OGR::Geometry.create_from_wkt(floyd_wkt, spatial_reference)
|
20
|
-
geometry.transform_to!(floyd.spatial_reference)
|
21
|
-
source_srs = geometry.spatial_reference
|
22
|
-
polygon = geometry.geometry_at 0
|
23
|
-
ring = polygon.geometry_at 0
|
24
|
-
|
25
|
-
extent = geometry.envelope
|
26
|
-
x_min = extent.x_min
|
27
|
-
x_max = extent.x_max
|
28
|
-
y_min = extent.y_min
|
29
|
-
y_max = extent.y_max
|
30
|
-
|
31
|
-
log "x_min: #{x_min}"
|
32
|
-
log "y_min: #{y_min}"
|
33
|
-
log "y_max: #{y_max}"
|
34
|
-
log "x_max: #{x_max}"
|
35
|
-
|
36
|
-
# extent = geometry.envelope.world_to_pixel(floyd.geo_transform)
|
37
|
-
# x_min = extent[:x_origin]
|
38
|
-
# x_max = extent[:x_max]
|
39
|
-
# y_min = extent[:y_origin]
|
40
|
-
# y_max = extent[:y_max]
|
41
|
-
# log "x_min: #{x_min}"
|
42
|
-
# log "y_min: #{y_min}"
|
43
|
-
# log "y_max: #{y_max}"
|
44
|
-
# log "x_max: #{x_max}"
|
45
|
-
|
46
|
-
|
47
|
-
# Create the destination data source
|
48
|
-
x_res = ((x_max - x_min) / pixel_size).to_i
|
49
|
-
y_res = ((y_max - y_min) / pixel_size).to_i
|
50
|
-
log "x_res: #{x_res}"
|
51
|
-
log "y_res: #{y_res}"
|
52
|
-
|
53
|
-
#target_ds = GDAL::Driver.by_name('MEM').create_dataset('', x_res, y_res, data_type: :GDT_Float32)
|
54
|
-
target_ds = GDAL::Driver.by_name('GTiff').create_dataset('grr.tif', x_res, y_res, data_type: :GDT_Float32)
|
55
|
-
|
56
|
-
geo_transform = floyd.geo_transform
|
57
|
-
geo_transform.x_origin = x_min
|
58
|
-
geo_transform.pixel_width = pixel_size
|
59
|
-
geo_transform.y_origin = y_max
|
60
|
-
geo_transform.pixel_width = -pixel_size
|
61
|
-
target_ds.geo_transform = geo_transform
|
62
|
-
target_ds.projection = floyd.projection
|
63
|
-
|
64
|
-
band = target_ds.raster_band(1)
|
65
|
-
band.no_data_value = no_data_value
|
66
|
-
band.fill(100)
|
67
|
-
|
68
|
-
#target_ds.rasterize_geometries!(1, geometry, 1, all_touched: 'TRUE')
|
69
|
-
target_ds.rasterize_geometries!(1, ring, 1, all_touched: 'TRUE')
|
70
|
-
|
71
|
-
band.to_na
|
72
|
-
|
73
|
-
|
74
|
-
binding.pry
|
75
|
-
|
76
|
-
target_ds.close
|
data/testing_nir.rb
DELETED
@@ -1,77 +0,0 @@
|
|
1
|
-
require 'bundler/setup'
|
2
|
-
require 'pry'
|
3
|
-
require 'ffi-gdal'
|
4
|
-
|
5
|
-
GDAL::Logger.logging_enabled = true
|
6
|
-
|
7
|
-
floyd_path = '/Users/sloveless/Development/projects/ffi-gdal/spec/support/images/Floyd/Floyd_1058_20140612_NRGB.tif'
|
8
|
-
floyd = GDAL::Dataset.open(floyd_path, 'r')
|
9
|
-
|
10
|
-
# dataset = floyd.extract_nir('nir.tif', 1)
|
11
|
-
# band = dataset.raster_band(1)
|
12
|
-
# colors = %w[644d1e 745924 856728 95742b a5812d b69930 c8b22d d8cb3c e8e65a
|
13
|
-
# f4ee79 e0e457 c8da42 afd135 97b73c 7e993c 657e36 4b612c 314441 23295e
|
14
|
-
# 282973]
|
15
|
-
# band.colorize!(*colors)
|
16
|
-
# binding.pry
|
17
|
-
# dataset.close
|
18
|
-
|
19
|
-
# dataset = floyd.extract_ndvi('ndvi_float.tif', band_order: %i[nir red green blue],
|
20
|
-
# data_type: :GDT_Float32,
|
21
|
-
# remove_negatives: true,
|
22
|
-
# photometric: 'PALETTE')
|
23
|
-
# dataset.close
|
24
|
-
|
25
|
-
# byte_dataset = floyd.extract_ndvi('ndvi_byte.tif', band_order: %i[nir red green blue],
|
26
|
-
# data_type: :GDT_Byte,
|
27
|
-
# remove_negatives: true,
|
28
|
-
# photometric: 'PALETTE')
|
29
|
-
# byte_dataset.close
|
30
|
-
#
|
31
|
-
# uint16_dataset = floyd.extract_ndvi('ndvi_uint16.tif', band_order: %i[nir red green blue],
|
32
|
-
# data_type: :GDT_UInt16,
|
33
|
-
# remove_negatives: true,
|
34
|
-
# photometric: 'PALETTE')
|
35
|
-
# uint16_dataset.close
|
36
|
-
#
|
37
|
-
|
38
|
-
colors = %w[644d1e 745924 856728 95742b a5812d b69930 c8b22d d8cb3c e8e65a
|
39
|
-
f4ee79 e0e457 c8da42 afd135 97b73c 7e993c 657e36 4b612c 314441 23295e
|
40
|
-
282973]
|
41
|
-
|
42
|
-
# byte_dataset = GDAL::Dataset.open('ndvi_byte.tif', 'w')
|
43
|
-
# byte_band = byte_dataset.raster_band(1)
|
44
|
-
# byte_band.colorize!(*colors)
|
45
|
-
# byte_dataset.close
|
46
|
-
|
47
|
-
# unit16_dataset = GDAL::Dataset.open('ndvi_uint16.tif', 'w')
|
48
|
-
# uint16_band = uint16_dataset.raster_band(1)
|
49
|
-
# uint16_band.colorize!(*colors)
|
50
|
-
# uint16_dataset.close
|
51
|
-
|
52
|
-
# g_uint16_dataset = floyd.extract_gndvi('gndvi_uint16.tif', band_order: %i[nir red green blue],
|
53
|
-
# data_type: :GDT_UInt16,
|
54
|
-
# remove_negatives: true,
|
55
|
-
# photometric: 'PALETTE'
|
56
|
-
# )
|
57
|
-
# g_uint16_dataset.close
|
58
|
-
#
|
59
|
-
# g_unit16_dataset = GDAL::Dataset.open('gndvi_uint16.tif', 'w')
|
60
|
-
# g_uint16_band = g_uint16_dataset.raster_band(1)
|
61
|
-
# g_uint16_band.colorize!(*colors)
|
62
|
-
# g_uint16_dataset.close
|
63
|
-
|
64
|
-
g_byte_dataset = floyd.extract_gndvi('gndvi_byte.tif', band_order: %i[nir red green blue],
|
65
|
-
data_type: :GDT_Byte,
|
66
|
-
remove_negatives: true,
|
67
|
-
photometric: 'PALETTE'
|
68
|
-
)
|
69
|
-
g_byte_dataset.close
|
70
|
-
|
71
|
-
g_byte_dataset = GDAL::Dataset.open('gndvi_byte.tif', 'w')
|
72
|
-
g_byte_band = g_byte_dataset.raster_band(1)
|
73
|
-
g_byte_band.colorize!(*colors)
|
74
|
-
binding.pry
|
75
|
-
g_byte_dataset.close
|
76
|
-
|
77
|
-
# floyd.extract_natural_color('nc.tif', band_order: %i[nir red green blue])
|